0

I want to use partial template specialization but it seems I am missing something. Here is what I've tried:

template<class T1, class T2>
class AClass {};

template<class T>
class AClass<T, T> {}; // specialized class.

AClass<int,float> aClassIntFloat; // works just fine

AClass<int, int> aClassIntInt; // works just fine

AClass<int> specializedIntClass; //"error: wrong number of template arguments (1, should be 2)"

What am I missing?

andmdnd
  • 13
  • 1
  • 4
    Indeed, it's a binary template, so why are you trying to instantiate it with but a single argument? Specify a default value for the second argument, or define and use an `using`-alias. – bipll Nov 22 '20 at 22:38
  • `AClass` *is* a specialised instantiation. – n. m. could be an AI Nov 22 '20 at 22:44
  • That was just a dummy example. What I really want is fairly more complex. What I don't get is why it doesn't compile. – andmdnd Nov 22 '20 at 23:09

1 Answers1

0

First of all, you have already successfully defined a partial specialistation:

  • AClass<int, int> will instantiate class AClass<T, T> with T=int
  • AClass<int,float> will instantiate class AClass<T1, T2> with T1=int and T2=float

You can easily check this by adding a public test method and invoking it for aClassIntFloat and aClassIntInt (online demo). You'll find out that the partial specialization defined for two identical types will be used.

You can also make another partial specialization, for example:

template<class T2>
class AClass<double, T2>  { public: void test(){cout<<"ADT2"<<endl;}};

AClass<double, int> aClassDoubleInt; // works also fine

A partial specialization lets you fix some parameters but not all. But in the end, your template requires two parameters, and you'll have to provide two parameters. The only question is which specialization if any gets instantiated (online demo).

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thanks for the answer. However, what I really want is more complex and that was just a dummy example. Regardless, I don't get why the version with a single template argument doesn't compile. When I read [this](https://en.cppreference.com/w/cpp/language/partial_specialization) I don't see the mistake. – andmdnd Nov 22 '20 at 23:12
  • @andmdnd The issue is that a partial or a full specialization does not define a new template class with different arguments (i.e. you didn't write `templateclass AClass {...};`). It does not redefine the arguments of the original template eithjer. It just defines some arguments that can be used when you specify ALL the arguments to be used for the specializationof your original template (this is why you had in the partial specialization for a single type T neverltheless explicitely to refer to `AClass`) – Christophe Nov 22 '20 at 23:31
  • 1
    Ah ok, now I got it. In the end I had the wrong idea about what it does. I thought it could make my life easier, in the sense of "typing less", when, say, both types are the same. – andmdnd Nov 22 '20 at 23:42