Is the following code valid?
struct B{ using X=int; };
struct D1:B{ using X=X; }; // (1)
struct D2:B{ typedef X X; }; // (2)
I would expect the point of declaration of D2::X to be between the two Xs in (2), but it seems both gcc 4.8 and clang 3.2 accept it.
Is this standard behavior ? References to the working draft/standard will be appreciated.
There was a debate on whether
using X = Xshould pick up the being definedXor theXthat potentially already was in scope. To avoid “unknown types” and to make it similar totypedef, it was ruled that the being-definedXis not visible in its to-be-assigned type expression (so rather than being similar toint x = x, it is similar totypedef x x;).Recall that
typedefis just a normal declaration with thetypedefkeyword prepended. The first mentioning ofXdoes not declare anything, it just says what type will be aliased. That’s the major difference withusing X = Xwhich could declareXearlier, if the committee decided that way.Note however your code is has effectively undefined behavior, because it violates a rule that has no required diagnostic. 3.3.7p1b2