2

Is it possible to specialize a class template by aliasing? I want to specialize a class template S for, say, double. But it is exactly same with existing type X<double, A, B> . So I want to use it succinctly.

template<class, class, class> X;
template<class, class> Y;

template<class> struct S;

template<> struct S<int> { ... }; // usual way to specialize

template<> using S<double> = X<double, A, B>; // what I want, but compile error.

template<> using S<float> = Y<C, D>; // what I want, but compile error.

I'd like to have something like the last line. Is there a way to achieve this?


This question is marked as a duplicate of Best way (Or workaround) to specialize a template alias. But the accepted answer of the question cannot be applied if there are other type pairs. For example, if S<float> sould be aliased with Y<C, D>, the method provided by the accepted answer does not work. I think that other answers for the question also do not provide answer for my question.

Balbadak
  • 21
  • 3
  • @DaveS it looks to me like the questions are not the same at all. `using` here is just placeholder syntax for "here, this specialization is actually the same as this one". The `using`-declaration itself is not specialized. – Quentin Apr 20 '18 at 12:48
  • 1
    This is achieved in type traits and other metaprogramming constructs by inheriting from the thing you should "alias". This can be impractical for "normal" classes though. – Quentin Apr 20 '18 at 12:52

1 Answers1

0

Would the following be an acceptable option?

template<class U, class V, class W> class X;
template<> class X<double,int, bool> {
  public:
   X() {}
};

template<class> struct S;

template<> struct S<float> {
  typedef X<double, int, bool> type;
};

int main() {
  S<float>::type s;
}
Kaveh Vahedipour
  • 3,412
  • 1
  • 14
  • 22