I was recently surprised to know that this code compiles (at least on gcc and MSVC++):
template<typename T>
class A {
public:
T getT() { return T(); }
};
class B : public A<B> { };
When this doesn't:
class A;
class B : public A { };
class A {
public:
B getB() { return B(); }
};
It seems weird to me that a template class could take an incomplete type as a template parameter and have a function that returned one by calling its constructor and still compile. So where exactly are complete types required (or if the list would be shorter, where are they not required)?
Following are the scenarios where Complete types are not required:
Basically You are fine using an Incomplete type, at any place where the compiler does not need to know the memory layout of the
type
.As for the template type argument being allowed to be an Incomplete type, the Standard explicitly says so in 14.3.1 Template type arguments