I want a struct to contain a type alias to another type for metaprogramming purposes:
struct Foo {};
struct WithNestedTypeAlias {
using Foo = Foo;
};
Then I can do stuff like WithNestedTypeAlias::Foo
in a template etc.
As I understand, this type alias is valid because it does not change the meaning of the Foo
type. Clang compiles this happily.
However, GCC complains:
test-shadow-alias.cpp:4:20: error: declaration of ‘using Foo = struct Foo’ [-fpermissive]
using Foo = Foo;
^
test-shadow-alias.cpp:1:8: error: changes meaning of ‘Foo’ from ‘struct Foo’ [-fpermissive]
struct Foo {};
^
Now I'm confused because I'm explicitly not changing the meaning of Foo
from struct Foo
.
What is the correct behaviour for C++14? I know I can work around this by renaming the struct Foo
, but I'd like to understand whether GCC's error is correct here.
Notes:
Tested with clang++ 3.8 and gcc 5.4, but Godbolt suggests this hasn't changed in more recent GCC versions.
I looked at Interaction between decltype and class member name shadowing an external name, where the name of a variable may refer to either a variable in the outer scope or to a class member. In contrast, my question here is about a type alias. There is no ambiguity since
Foo
always refers to::Foo
within the class scope. I don't see how the answer there applies to my problem.This is probably due to a misunderstanding of what type aliases actually are.