This is how CRTP works due to two stage template parsing. Template member functions are not parsed until their instantiation.
EDIT: Maybe, the wording is not very precise. What I meant to say that when compiler sees
class B : public A< B > {...};
, it goes through A< B >
, notices that there is a function B get() {...}
, but does not evaluate its definition, leaving it until function's actual instantiation, at which point B
has to be a complete type.
EDIT: I believe, the exact rules are covered in Standard section 14.6 (as Als pointed out in his answer). It deals with dependent
and non-dependent
names and their resolution at different times during compilation according to two-phase template name look-up. However, unfortunately, the two-phase name look-up implementation can differ from Standard on different compilers. Same code might compile on GCC and might not on MSVC++ and vice versa. Even more, seemingly same code might be rejected by same compiler. On MSVC++ I had an issue when base class was using a pointer to derived class function as a default argument for its function. It did not compile under MSVC++ and compiled under GCC (correctly). However, using derived class constructor as a default parameter compiled with both compilers, even on MSVC++. Go figure.