Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent non-specialized template instantiation?

I have a templated class (call it Foo) which has several specializations. I would like the compilation to fail if someone tries to use an unspecialized version of Foo.

Here is what I actually have:

template <typename Type> class Foo {   Foo() { cannot_instantiate_an_unspecialized_Foo(); }    // This method is NEVER defined to prevent linking.   // Its name was chosen to provide a clear explanation why the compilation failed.   void cannot_instantiate_an_unspecialized_Foo(); };  template <> class Foo<int> {    };  template <> class Foo<double> {    }; 

So that:

int main() {   Foo<int> foo; } 

Works while:

int main() {   Foo<char> foo; } 

Does not.

Obviously, the compiler chain only complains when the linking process takes place. But is there a way to make it complain before ?

I can use boost.

like image 221
ereOn Avatar asked Aug 15 '11 10:08

ereOn


People also ask

What causes a C++ template function to be instantiated?

When a function template is first called for each type, the compiler creates an instantiation. Each instantiation is a version of the templated function specialized for the type. This instantiation will be called every time the function is used for the type.

What is instantiation of a template?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation to handle a specific set of template arguments is called a specialization.

What is the difference between generic class template and specialization template?

Key differences between generics and C++ templates: Generics are generic until the types are substituted for them at runtime. Templates are specialized at compile time so they are not still parameterized types at runtime. The common language runtime specifically supports generics in MSIL.

What is Typename in template?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.


2 Answers

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.

like image 169
Seb Holzapfel Avatar answered Sep 21 '22 05:09

Seb Holzapfel


You can simply not define the base case:

template <typename> class Foo;             // no definition!  template <> class Foo<int> { /* ... */ };  // Foo<int> is OK 
like image 31
Kerrek SB Avatar answered Sep 18 '22 05:09

Kerrek SB