Why the incomplete type can be new from the base class with template technique

Viewed 58

The following code will fail

class InComplte;
void f() {
    new InComplte();
}
class InComplte {

};

Because it is newing an incomplete type. But if template is used, the following code can compile

template<typename T>
class CComCoClass {
    public:
    static void create_di(T**p) {
        *p = new T();
    }

};

class Book: public CComCoClass<Book> {
public:
int i = 3;

};

The base class CComCoClass seems to be newing an incomplete type. Why the Book can inherit a base class that creates it?

1 Answers

A member function of a class template is not evaluated when the class template is instantiated. The functions instantiation is delayed until it is called. create_di isn't called in the example, so it isn't evaluated.

Related