3

While template specialization is allowed, one can't specialize a template using directive. What are some tricks to achieve it anyway ?

e.g :


this is ok :

template <class A>
class MyTemplate { ... };

template <>
class MyTemplate<int> { ... };

this isn't :

template <class A>
using Alias = SomeClass<A>;

template <>
using Alias<int> = MyBigIntClass;

EDIT:

The goal is to use Alias<int> in the client code, and have it be MyBigIntClass under the hood.

Julien__
  • 1,962
  • 1
  • 15
  • 25

1 Answers1

3

Here is the trick (use template specialization) :

template <class A>
struct Helper{              //default case
    using t = SomeClass<A>; //use void or omit this line to disable default case
};

template <class A>
using Alias = typename Helper<A>::t;

template <>
struct Helper<int>{
    using t = MyBigIntClass; //whatever you need
};

int main()
{
    Alias<int> a = 0; //It's MyBigIntClass under the hood
}
Julien__
  • 1,962
  • 1
  • 15
  • 25