Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using template specialization

Usual template structs can be specialized, e.g.,

template<typename T>
struct X{};

template<>
struct X<int>{};

C++11 gave us the new cool using syntax for expressing template typedefs:

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

Is there a way to define a template specialization for these using constructs similar to specializations for struct templates? I tried the following:

template<>
using YetAnotherVector<int> = AFancyIntVector;

but it yielded a compile error. Is this possible somehow?

like image 965
gexicide Avatar asked Nov 10 '14 13:11

gexicide


1 Answers

No.

But you can define the alias as:

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

Hope that helps.

like image 137
Nawaz Avatar answered Sep 29 '22 19:09

Nawaz