c++ - 将类型转换为整数的元函数,反之亦然

标签 c++ templates c++11 types template-meta-programming

typeid 允许在运行时为每个类型分配一个唯一的 std::type_index。我想做同样的事情,静态地使用两个元函数:

// Get a unique integral number associated with the provided type
template <class T>
struct encode_type
{
    using type = T;
    static constexpr std::size_t value = /* Metaprogramming magic */;
};

// Get the type uniquely associated with the provided value
template <std::size_t V>
struct decode_type
{
    static constexpr std::size_t value = V;
    using type = /* Metaprogramming magic */;
};

有没有办法在 C++11 中做到这一点?

最佳答案

这是一个“适用于”GCC 5.2 和 Clang 3.7 的可能解决方案。

我使用 Filip RoséenConstexpr Meta-Container有一些细微的改动。作为 T.C.指出,这可能是made ill-formed在未来,所以这个解决方案在生产代码中是完全不合理的,但它现在很酷。我什至不确定这是否 100% 符合标准。

// This is our meta-container
using TypeMap = atch::meta_list<class A>;

// Get a unique integral number associated with the provided type
template <class T>
struct encode_type
{
    using type = T;
    // Push T into the container and store the pre-push size
    //( requires slight change to Filip's code)
    static constexpr std::size_t value = TypeMap::push<T>();
};

// Get the type uniquely associated with the provided value
template <std::size_t V>
struct decode_type
{
    static constexpr std::size_t value = V;
    // Get the type at index V
    // (requires a small helper function addition)
    using type = decltype(TypeMap::at<V>());
};

我对原始代码所做的改动:

template<class T, class H = meta_list, std::size_t Size = counter::value()>
static constexpr std::size_t push (
  size_type = push_state<
    typename H::template value<>::template push<T>::result
  > ()
) { return Size; } 

我修改了 atch::meta_list::push 以返回推送之前 元容器的大小。我使用带有默认参数的模板参数来确保在推送之前计算大小。

template<size_type Idx, class H = meta_list>
static constexpr auto at () -> typename H::template value<>::template at<Idx>::result;

我在 atch::meta_list 中添加了一个小的 decltype 辅助函数,以隐藏所有相关的名称困惑。


部分测试代码:

int main () {
    std::array<int, 4> encoded { 
        encode_type<int>::value,
        encode_type<double>::value,
        encode_type<std::string>::value,
        encode_type<float>::value
    };
  
  std::cout << "Encoding: ";
  for (auto i : encoded) std::cout << i << ", ";
  std::cout << std::endl;
  
  std::array<std::type_index, 4> decoded {
      typeid(decode_type<0>::type),  
      typeid(decode_type<1>::type),
      typeid(decode_type<2>::type),
      typeid(decode_type<3>::type),
  };
  
  std::cout << "Decoding: ";
  for (auto i : decoded) std::cout << i.name() << ", ";
  std::cout << std::endl;
}

Clang 和 GCC 都会发出一堆警告,但它们都“有效”!

铿锵 compiles, runs and outputs :

Encoding: 0, 1, 2, 3,

Decoding: i, d, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, f,

海湾合作委员会 compiles, runs and outputs :

Encoding: 0, 1, 2, 3,

Decoding: i, d, NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE, f,


也许预处理步骤会更好...

关于c++ - 将类型转换为整数的元函数,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230174/

相关文章:

c++ - 模板类上的二元运算符重载

c++ - 在元函数定义中使用 mpl::vector

c++ - 编译器使用哪些真正的规则集来决定是否内联函数?

c++ - 关于未声明 TITLEBARINFO 的 MinGW 错误

c++ - 模板类变量声明中的 "class VARIABLE"?

c++ - OpenCV 模板化代码在派生类中产生编译错误

c++ - 双向 map 是否有更有效的实现?

c++ - 使用 C++ 正则表达式库中的 regex_search

c++ - 何时使用 C++11 mutex、lock、unique_lock、shared_lock 等

c++ Qt/Win32 - USB 以编程方式弹出,但仍显示在 QFileSystemModel 和 GetLogicalDrives() 中