How to prevent non-specialized template instantiation?

Just don’t define the class:

template <typename Type>
class Foo;

template <>
class Foo<int> { };

int main(int argc, char *argv[]) 
{
    Foo<int> f; // Fine, Foo<int> exists
    Foo<char> fc; // Error, incomplete type
    return 0;
}

Why does this work? Simply because there isn’t any generic template. Declared, yes, but not defined.

Leave a Comment