13

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?

gexicide
  • 38,535
  • 21
  • 92
  • 152

3 Answers3

11

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

It's neither possible to specialize them explicitly nor partially. [temp.decls]/3:

Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template.

You will have to defer specializations to class templates. E.g. with conditional:

template<typename T>
using YetAnotherVector = std::conditional_t< std::is_same<T, int>{}, 
                                             AFancyIntVector, 
                                             std::vector<T> >;
Columbo
  • 60,038
  • 8
  • 155
  • 203
  • 1
    If you're using C++14, then you could avoid writing `::value` part as well, as `std::is_same{}` is shorter! – Nawaz Nov 10 '14 at 13:12
  • @Nawaz I don't just care about shortness but also clarity. Anyway, if people find that readable, I'll edit the code appropriately – Columbo Nov 10 '14 at 13:13
  • Umm, you do care about shortness, which is why you wrote `_t` version of `std::conditional`. – Nawaz Nov 10 '14 at 13:15
  • @Nawaz No, that is because of readability (=clarity). `conditional_t<…>` is more readable than `typename conditional<…>::type`, don't you think? – Columbo Nov 10 '14 at 13:15
  • And you think `{}` version isn't better in terms of clarity? Think about lots of metaprogramming and the horizontal space it eats up! – Nawaz Nov 10 '14 at 13:16
  • @Nawaz I personally think `::value` expresses the intention more clearly, but that opinion will probably change rather soon. :) – Columbo Nov 10 '14 at 13:16
  • Yes, it is likely your opinion will change soon. :-) – Nawaz Nov 10 '14 at 13:17
0

There is another pattern which I prefer over the std::conditional. Maybe you'll prefer it as well.

It looks like this:

struct MyAwesomeVector {};

// this is the default 
template<typename T>
struct VectorSpecialization {
    typedef std::vector<T> Type;
} ;

// this is your awesomeness for int
template<>
struct VectorSpecialization<int> {
    typedef MyAwesomeVector Type;
} ;

// this is the using declaration
template<typename T>
using MyVector = typename VectorSpecialization<T>::Type;
iamacomputer
  • 518
  • 3
  • 14