Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
Jamboree Avatar asked Aug 26 '15 07:08

Jamboree


People also ask

What types of declarations are not allowed in the C++ language?

any number of variable declarations, bit field declarations, and static assert declarations. Members of incomplete type and members of function type are not allowed (except for the flexible array member described below)

What is the general form of structure declaration?

The general form of structure declaration is as follows − struct is the keyword. member1, member2 specifies the data items that makeup structure. There are three ways of declaring structure variables, which are as follows −

How many qualified names can be used in member declarations?

No qualified names to use in member declarations. Which compiler is used in your book ?

What is the scope of a struct declaration?

Because a struct declaration does not establish scope, nested types, enumerations and enumerators introduced by declarations within struct-declaration-list are visible in the surrounding scope where the struct is defined.


1 Answers

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.

like image 132
TartanLlama Avatar answered Nov 15 '22 18:11

TartanLlama