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?
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)
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 −
No qualified names to use in member declarations. Which compiler is used in your book ?
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.
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 classS
shall refer to the same declaration in its context and when re-evaluated in the completed scope ofS
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With