Jamboree
Jamboree

Reputation: 5329

Is member declaration `decltype(name) name;` permitted in local struct where the first name refers to the enclosing scope?

Example:

int main()
{
    int a = 0;
    struct X
    {
        decltype(a) a;
    };
    return 0;
}

The decltype(a) refers to the local a in main, while the member it declares shares the same name.

Clang compiles w/o any problem, so does MSVC14.

G++ complains on it, adding -fpermissive makes it pass though

prog.cc:6:21: error: declaration of 'int main()::X::a' [-fpermissive]
         decltype(a) a;
                     ^
prog.cc:3:9: error: changes meaning of 'a' from 'int a' [-fpermissive]
     int a = 0;

Which behavior is standard-conformant?

Upvotes: 10

Views: 196

Answers (1)

TartanLlama
TartanLlama

Reputation: 65770

I believe this violates [basic.scope.class]/1 (N3337):

The following rules describe the scope of names declared in classes.

1) [...]

2) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

Since decltype(a) refers to the declaration in the enclosing scope before the member variable is declared, but refers to the member when "re-evaluated in the completed scope of" X, the program is ill-formed. No diagnostic is required, but GCC provides one anyway (although it's fairly arcane). The behaviour of all three compilers is valid.

Upvotes: 8

Related Questions