Lauren S
Lauren S

Reputation: 319

Workaround for the lack of support for type alias specialization in C++

I want to be able to refer to different types using literals as ids.

template<auto>
using type = void;

template<>
using type<0> = int;

template<>
using type<1> = char;

template<>
using type<2> = string;

int main()
{
  type<0> var0;
  type<1> var1;
  type<2> var2;
}

This results in the compiler giving me errors, since type alias specialization is not yet supported in C++. (The technology required to implement such a feature simply does not exist)

Upvotes: 3

Views: 239

Answers (4)

Evg
Evg

Reputation: 26292

If you don't need a default void value, here is a simple std::tuple-based solution:

template<std::size_t I>
using type = std::tuple_element_t<I, std::tuple<int, char, std::string>>;

Upvotes: 0

Pepijn Kramer
Pepijn Kramer

Reputation: 12891

This will do it, and will give you the syntax you need. Note I explicitly use std::size_t to avoid specialization on other types than numbers.

#include <string>
#include <type_traits>

//-------------------------------------------------------------------
// hide all the boiler plate in a namespace
// use structs for partial specializations

namespace details
{
    template<std::size_t N>
    struct type_s { using type = void; };

    template<> struct type_s<0> { using type = int; };
    template<> struct type_s<1> { using type = char; };
    template<> struct type_s<2> { using type = std::string; };
}

//-------------------------------------------------------------------
// now you can use a full template for alias

template<std::size_t N>
using type_t = typename details::type_s<N>::type;

//-------------------------------------------------------------------

int main()
{
    type_t<0> var0{ 42 };
    type_t<1> var1{ 'A' };
    type_t<2> var2{ "Hello World!" };

    static_assert(std::is_same_v<type_t<0>, int>);
    static_assert(std::is_same_v<type_t<1>, char>);
    static_assert(std::is_same_v<type_t<2>, std::string>);

    static_assert(std::is_same_v<decltype(var0), int>);
    static_assert(std::is_same_v<decltype(var1), char>);
    static_assert(std::is_same_v<decltype(var2), std::string>);
}

Upvotes: 12

Ruks
Ruks

Reputation: 3956

Partial specialization is not allowed in alias templates, however, you can use std::conditional instead:

#include <type_traits>
#include <string>
// ...

template <auto X>
using type = std::conditional_t<X == 0, int,
             std::conditional_t<X == 1, char,
             std::conditional_t<X == 2, std::string,
                 void>>>;

Upvotes: 6

Lauren S
Lauren S

Reputation: 319

Luckily, I found a workaround which uses class specialization which is supported in C++:

template<auto>
struct TypeAliasWrapper;

template<>
struct TypeAliasWrapper<0>{using type = int;};

template<>
struct TypeAliasWrapper<1>{using type = char;};

template<>
struct TypeAliasWrapper<2>{using type = string;};

int main()
{
  TypeAliasWrapper<0>::type var0;
  TypeAliasWrapper<1>::type var1;
  TypeAliasWrapper<2>::type var2;
}

Upvotes: 0

Related Questions