Using Specialized Specialization

Conventional template structures can be specialized, for example,

template<typename T> struct X{}; template<> struct X<int>{}; 

C ++ 11 gave us a new cool using syntax for expressing typedefs templates:

 template<typename T> using YetAnotherVector = std::vector<T> 

Is there a way to define template specialization for them using constructs similar to those for structure templates? I tried the following:

 template<> using YetAnotherVector<int> = AFancyIntVector; 

but it gave a compilation error. Is it possible somehow?

+6
source share
2 answers

No.

But you can define an alias as:

 template<typename T> using YetAnotherVector = typename std::conditional< std::is_same<T,int>::value, AFancyIntVector, std::vector<T> >::type; 

Hope this helps.

+7
source

It is not possible to specialize them explicitly or in part. [Temp.decls] / 3:

Since the alias declaration cannot declare a template identifier, it is not possible to partially or explicitly specialize an alias template.

You will have to defer specialization to class templates. For instance. with conditional :

 template<typename T> using YetAnotherVector = std::conditional_t< std::is_same<T, int>{}, AFancyIntVector, std::vector<T> >; 
+1
source

Source: https://habr.com/ru/post/977981/


All Articles