1

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.

Sayan
  • 2,662
  • 10
  • 41
  • 56
  • I don't quite get it. If they have default values then the default values are used. If the values are combined they're not default anymore, are they? – Pixelchemist Jun 28 '16 at 14:37
  • For eg.,for the first example -- `TNT v`, the default values for non-types shall be used. But, for the second example, for the second and third parameter, the user passes the values. – Sayan Jun 28 '16 at 14:41

2 Answers2

5

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

If you are talking about explicit instantiation in its usual sense, then I presume your question is about avoiding linking errors when you define template class implementation in a cpp file rather than a header file.

If that is the case, then usual explicit instantiation rules apply, with this syntax:

template class TNT<int>;
template class TNT<float, 1.0, 2.0>;
//etc

You will need to write as many of these as the combinations used elsewhere require.

Smeeheey
  • 9,906
  • 23
  • 39
  • Yes, your assumption is correct. But, as @Nicol Bolas says - `There is no syntax that will instantiate a template for every possible combination of parameter values.` – Sayan Jun 28 '16 at 14:52
  • No, indeed there isn't. As I said you will need to explicitly write as many of these as you require. – Smeeheey Jun 28 '16 at 14:53