相关疑难解决方法(0)

如何创建类型列表的笛卡尔积?

我想使用可变参数模板创建类型列表的交叉产品.

这是我到目前为止所拥有的:

#include <iostream>
#include <typeinfo>
#include <cxxabi.h>

template<typename...> struct type_list {};

template<typename T1, typename T2> struct type_pair {};

template<typename T, typename... Rest>
  struct row
{
  typedef type_list<type_pair<T,Rest>...> type;
};

template<typename... T>
  struct cross_product
{
  typedef type_list<typename row<T,T...>::type...> type;
};

int main()
{
  int s;
  typedef cross_product<int, float, short>::type result;
  std::cout << abi::__cxa_demangle(typeid(result).name(), 0, 0, &s) << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该计划输出:

$ g++ -std=c++0x cross_product.cpp ; ./a.out 
type_list<type_list<type_pair<int, int>, type_pair<int, float>, type_pair<int, short> >, type_list<type_pair<float, int>, type_pair<float, float>, type_pair<float, …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

12
推荐指数
2
解决办法
2686
查看次数

标签 统计

c++ ×1

c++11 ×1

variadic-templates ×1