想要类型上的 constexpr 开关盒

Want kind of constexpr switch case on type

我目前正在使用这个技巧来获得基于类型的 cstring:

template<class ListT> static char constexpr * GetNameOfList(void)
{
  return
  std::conditional<
    std::is_same<ListT, LicencesList>::value, "licences",
    std::conditional<
      std::is_same<ListT, BundlesList>::value, "bundles",
      std::conditional<
        std::is_same<ListT, ProductsList>::value, "products",
        std::conditional<
          std::is_same<ListT, UsersList>::value, "users",
          nullptr
        >
      >
    >
  >;
}

但是这段代码不太好看,如果我们要检查更多的类型,这可能是不可读的。是否可以像有一个 switch case 块一样做同样的事情?

实际上,代码比这更复杂,因为 std::conditional 需要一些类型,所以我们需要一些 class 来完成这个技巧:

struct LicenceName { static char constexpr * value = "licences"; };

例如。

我认为使用模板专业化会更容易


示例代码:

#include <iostream>
struct A{};
struct B{};
struct C{};
struct D{};

template<typename T> constexpr const char* GetNameOfList();
//here you may want to make it return nullptr by default

template<>constexpr const char* GetNameOfList<A>(){return "A";}
template<>constexpr const char* GetNameOfList<B>(){return "B";}
template<>constexpr const char* GetNameOfList<C>(){return "C";}

int main(){
   std::cout << GetNameOfList<A>() << '\n';
   std::cout << GetNameOfList<B>() << '\n';
   std::cout << GetNameOfList<C>() << '\n';
   //std::cout << GetNameOfList<D>() << '\n'; //compile error here
}

你不需要求助于元编程,简单的 ifs 工作得很好:

template<class ListT>
constexpr char const *GetNameOfList() {

    if(std::is_same<ListT, A>::value) return "A";
    if(std::is_same<ListT, B>::value) return "B";
    if(std::is_same<ListT, C>::value) return "C";
    if(std::is_same<ListT, D>::value) return "D";

    return nullptr;
}

See it live on Coliru

您可以创建字符串的 constexpr 数组加上列表类型的元组来创建映射 list type -> index -> name(如果您需要映射 index -> types containing strings,只需使用元组而不是数组)。 c++17 方法可能如下所示:

#include <type_traits>
#include <tuple>
#include <utility>
#include <iostream>

struct LicencesList{};
struct BundlesList{};
struct ProductsList{};
struct UsersList{};

using ListTypes = std::tuple<LicencesList, BundlesList, ProductsList, UsersList>;
constexpr const char *NameList[] = {"licences", "bundles", "products", "users"};

template <class Tup, class, class = std::make_index_sequence<std::tuple_size<Tup>::value>>
struct index_of;

template <class Tup, class T, std::size_t... Is>
struct index_of<Tup, T, std::index_sequence<Is...>> {
    static constexpr std::size_t value = ((std::is_same<std::tuple_element_t<Is, Tup>, T>::value * Is) + ...);
};


template<class ListT> static const char constexpr * GetNameOfList(void) {
  return NameList[index_of<ListTypes, ListT>::value];
}

int main() {
    constexpr const char *value = GetNameOfList<BundlesList>();
    std::cout << value << std::endl;
}

[live demo]

如果你想保持 c++11 兼容性,方法会稍微长一点(我在这里使用 Casey's answer 来实现 index_of 结构):

#include <type_traits>
#include <tuple>
#include <iostream>

struct LicencesList{};
struct BundlesList{};
struct ProductsList{};
struct UsersList{};

using ListTypes = std::tuple<LicencesList, BundlesList, ProductsList, UsersList>;
constexpr const char *NameList[] = {"licences", "bundles", "products", "users"};

template <class Tuple, class T>
struct index_of;

template <class T, class... Types>
struct index_of<std::tuple<T, Types...>, T> {
    static const std::size_t value = 0;
};

template <class T, class U, class... Types>
struct index_of<std::tuple<U, Types...>, T> {
    static const std::size_t value = 1 + index_of<std::tuple<Types...>, T>::value;
};


template<class ListT> static const char constexpr * GetNameOfList(void) {
  return NameList[index_of<ListTypes, ListT>::value];
}

int main() {
    constexpr const char *value = GetNameOfList<BundlesList>();
    std::cout << value << std::endl;
}

[live demo]