Consider the following "code":
struct T
{
T();
static int x;
};
T::T::T::T::T::T()
{
}
int T::x;
int main()
{
T t1;
T::T t2;
T* p=static_cast<T::T::T::T*>(&t1);
T::T::T::T::T::x=5;
}
It compiles with both g++ from 4.7 to 8 and clang++ from 3.5 to 6 without any errors or relevant warnings with -pedantic-errors
. Apparently, class name followed by scope resolution operator can be repeated without affecting meaning of the class type being named — as if the class is nested inside itself.
Is it a bug in compilers? Or is the C++ language really defined in such a way that it tolerates such abuse?
If it's really standard-conforming C++ code, is there any rationale for allowing this, or is it just to simplify wording of the Standard?