Home > Back-end >  Workaround for the lack of support for type alias specialization in C
Workaround for the lack of support for type alias specialization in C

Time:09-22

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)

CodePudding user response:

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 then 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>);
}

CodePudding user response:

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>>>;

CodePudding user response:

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;
}
  • Related