This code
int clash;
struct Foo {
decltype(clash) clash;
};
compiles silently on clang, but fails to compile on gcc giving the errors
error: declaration of 'int Foo::clash' [-fpermissive]
error: changes meaning of 'clash' from 'int clash' [-fpermissive]
It seems that 2 ingredients are required for the error to arise:
The shadowing must be done by a class member (no problem if it's a function's local scope).
decltype([shadowed name]) must be used in the shadowing scope before the declaration of [shadowing name].
My question is twofold:
- Is gcc justified in rejecting this code?
- Where does it say so in the standard?
gcc
is correct the program is ill-formed, although this particular violation does not require a diagnostic soclang
does not have to provide one.If we look at the C++11 standard(The closest draft would be N3337) section
3.3.7
Class scope it says:and the next rule says:
It makes sense we would want to prevent situations where reordering the declarations in a class give a different program. It is curious whether these two rules are redundant or not.
The section also provides the following example:
and if we try this example with
gcc
(see it live), we get an almost identical error to one your code produces: