I am working on a large project that contains a section of code that compiles, but I do not understand how to do this. I overdid up to this simple example:
template <typename T> struct First { typedef int type; // (A) typename T::Three order; // (B) }; template <typename T> struct Second { typedef typename T::type type; }; template <typename T> struct Third { int val; T two; }; struct Traits { typedef First<Traits> One; typedef Second<One> Two; typedef Third<Two> Three; }; int main(int argc, char** argv) { Traits::One x; };
The First class is a template on Traits and links are Traits::Three , which itself is a typedef based on Two , which is a typedef based on First<Traits> ... therefore, it is circular. But this code compiles fine on both gcc4.6 and VC10. However, if I flip the ordering of the two lines marked (A) and (B) , the code does not compile, complaining of a typedef inside Second .
Why is this code compiling and why does the ordering of the typedef variable and the member matter?
c ++ templates
Barry
source share