What does C++ syntax struct A::B:A {};
mean? Where is this name definition (or access) described in the C++ standard?
#include <iostream>
struct B;
struct A {
struct B;
};
struct A::B:A {
};
int main() {
A::B::A::B b;
std::cout<<"Sizeof A::B::A::B is " << sizeof(A::B::A::B)<<std::endl;
return 0;
}
This definition
Defines a struct
A
with a declaration of a nested structB
1. The fully qualified name ofB
isA::B
, you could sayB
is inside the "namespace" ofA
. Then this:Is the definition of
A::B
, and the single:
specifies that it is derived fromA
.Now, the interesting part is
A::B::A::B
. Let's dissect it:A::B
names the nested structure.A::B::A
accesses the injected class nameA
insideB
. The injection is due to the inheritance.A::B::A::B
names the nested structureB
inA
again.And you can continue ad-infinitum, or at least until your compiler meets its translation limit2.
A fun intellectual exercise, but avoid like the plague in actual code.
[class.qual]/1 explains how the lookup works
And the text above allows us to name the base class because [class]/2
The above clearly says that starting a fully qualified name with
A::
allows you to specify a member or a base class. SinceA
has no bases, you can only specifyA::B
(a "member type"). ButA::B
also nominates a class. So we may specify a base or member of that as well withA::B::
, which allows us to nameA::B::A
. Now rinse and repeat.1 - Note it's a completely other
B
. Not at all related to the globalstruct B
.2 - A recommended minimum of 256 according to [implimits]/2.36