我想使用可变参数模板创建类型列表的交叉产品.
这是我到目前为止所拥有的:
#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) 我试图创建一些工具来创建基于其他类型组合的类型列表。
可以说我们有三种类型
struct A{};
struct B{};
struct C{};
Run Code Online (Sandbox Code Playgroud)
我想获得一个元组列表,其中包含N,A,B或C个类型的所有可能组合。
对于N = 2的情况,这将是
std::tuple<A,A>
std::tuple<A,B>
std::tuple<A,C>
std::tuple<B,A>
std::tuple<B,B>
std::tuple<B,C>
std::tuple<C,A>
std::tuple<C,B>
std::tuple<C,C>
Run Code Online (Sandbox Code Playgroud)
这个想法是创建一个元组来容纳所有这些类型的容器,因此我以后可以在容器列表中存储任何这些类型。
template <typename ...Combinations>
using CombinationList = std::tuple<std::vector<Combinations>...>;
Run Code Online (Sandbox Code Playgroud)
我已经有一种机制可以在适合的容器中插入一个特殊的元素,但是我对如何创建组合一无所知。
在人们建议使用的评论上std::vector<Combination<std::variant<A,C,B>, std::variant<A,B,C>>>。尽管这从技术上解决了问题,但我不喜欢使用它,因为A,BC的大小非常不同,并且我不想在运行时访问变体。另外,在某些时候,我需要将所有数据上传到
std::tuple<std::vector<Combination>...>
Run Code Online (Sandbox Code Playgroud)
到GPU,所以我不能在这里使用std :: variant。
我该怎么办?
谢谢!
PD:这与一个问题有关(一个枚举值(729个组合...)的组合爆炸)。 在这个问题中,我问我如何轻松生成将放入容器内的类型。现在我需要生成容器。
c++ cartesian-product template-meta-programming variadic-templates c++14
我有闲置的测试集:
TEST_F(FactoryShould, createAFromAModule)
{
const auto stateMachine = createStateMachine(EModule_A);
const auto* typedStateMachine =
dynamic_cast<const BackEnd<transitiontable::A, Guard>*>(stateMachine.get());
ASSERT_TRUE(typedStateMachine);
}
TEST_F(FactoryShould, createBFromBModule)
{
const auto stateMachine = createStateMachine(EModule_B);
const auto* typedStateMachine =
dynamic_cast<const BackEnd<transitiontable::B, Guard>*>(stateMachine.get());
ASSERT_TRUE(typedStateMachine);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法把它们变成类型化测试?我所看到的只是一个更改参数的解决方案,我有 2 个更改参数,EModule 可用于多个转换表,所以像 map 这样的东西看起来不错,但它可行吗?