Search code examples
c++c++11explicit-instantiation

How to explicitly instantiate class with default template arguments in C++?


I have a class with type and non-type(default) template parameters. The non-type parameters could be combined, and can be instantiated in the following ways:

TNT<int> v;
TNT<double, X, Y> v2;
TNT<float, X | X1, Y1> v3;
TNT<int, X | X1, Y | Y1, Z | Z1 | Z2, W> v4;

The class TNT has a type parameter, and rest are default. What is the correct way to explicitly instantiate such a class in the cpp file? Since the non-type parameters could be combined, a lot of combinations are possible.


Solution

  • I think the thing you're not getting is this. TNT<int, 5> is a completely different type from TNT<int, 4>. They are as different from each other as vector<int> is from vector<float>.

    As such, you cannot instantiate all possible non-type parameters. If you instantiate TNT<int> then you are instantiating the specific template that uses the default parameters. If your default params were 1, 2, 3, then TNT<int> would be equivalent to TNT<int, 1, 2, 3>.

    But that's it. There is no syntax that will instantiate a template for every possible combination of parameter values.