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
Awith a declaration of a nested structB1. The fully qualified name ofBisA::B, you could sayBis 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::Bnames the nested structure.A::B::Aaccesses the injected class nameAinsideB. The injection is due to the inheritance.A::B::A::Bnames the nested structureBinAagain.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. SinceAhas no bases, you can only specifyA::B(a "member type"). ButA::Balso 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