Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C+中的容器元组中提取value_类型的元组+;11 我有一个模板参数,它是一个不同的元素类型的标准C++容器的代码: 如何从中提取元素类型的std::tuple_C++_Templates_C++11_Boost_Boost Fusion - Fatal编程技术网

从C+中的容器元组中提取value_类型的元组+;11 我有一个模板参数,它是一个不同的元素类型的标准C++容器的代码: 如何从中提取元素类型的std::tuple

从C+中的容器元组中提取value_类型的元组+;11 我有一个模板参数,它是一个不同的元素类型的标准C++容器的代码: 如何从中提取元素类型的std::tuple,c++,templates,c++11,boost,boost-fusion,C++,Templates,C++11,Boost,Boost Fusion,例如,假设我有以下函数 template <typename TupOfCtrs> void doStuff(const TupOfCtrs& tupOfCtrs) { using TupOfElements = /*extract a tuple type by applying CtrT::value_type to each container in tupOfCtrs and combining the results into an std::tuple*/

例如,假设我有以下函数

template <typename TupOfCtrs>
void doStuff(const TupOfCtrs& tupOfCtrs) {
    using TupOfElements = /*extract a tuple type by applying CtrT::value_type to each container in tupOfCtrs and combining the results into an std::tuple*/;
    MyHelperClass<TupOfElements> helper;
}
模板
无效剂量凝灰岩(常数TupOfCtrs和TupOfCtrs){
使用tupofeelements=/*通过将CtrT::value_type应用于tupOfCtrs中的每个容器并将结果组合成std::tuple*/,提取一个tuple类型;
MyHelper类助手;
}
我知道人们这样称呼它:

std::list<Foo> l {/*...*/};
std::vector<Bar> v {/*...*/};
std::deque<Baz> d {/*...*/};
auto tup = std::make_tuple(l, v, d);
// Template which takes one type argument:
template <typename Tuple> struct TupOfValueTypes;

// Only provide a definition for this template for std::tuple arguments:
// (i.e. the domain of this template metafunction is any std::tuple)
template <typename ... Ts>
struct TupOfValueTypes<std::tuple<Ts...> > {
    // This definition is only valid, if all types in the tuple have a
    // value_type type member, i.e. the metafunction returns a type only
    // if all types of the members in the std::tuple have a value_type
    // type member, and a std::tuple can be constructed from these:
    using type = std::tuple<typename Ts::value_type...>;
};

template <typename TupOfCtrs>
void doStuff(const TupOfCtrs& tupOfCtrs) {
    using TupOfElements = typename TupOfValueTypes<TupOfCtrs>::type;
    // ...
}
std::list l{/*…*/};
std::向量v{/*…*/};
std::deque d{/*…*/};
auto tup=std::make_tuple(l,v,d);
在本例中,我希望将
tupofeelements
helper类型定义为
std::tuple
。 请注意,我不需要实际创建元组,只需要获取其类型


如何实现这一点,可能需要使用库?

您甚至可以通过更简单的方式来实现,而无需像这样使用Boost Fusion:

std::list<Foo> l {/*...*/};
std::vector<Bar> v {/*...*/};
std::deque<Baz> d {/*...*/};
auto tup = std::make_tuple(l, v, d);
// Template which takes one type argument:
template <typename Tuple> struct TupOfValueTypes;

// Only provide a definition for this template for std::tuple arguments:
// (i.e. the domain of this template metafunction is any std::tuple)
template <typename ... Ts>
struct TupOfValueTypes<std::tuple<Ts...> > {
    // This definition is only valid, if all types in the tuple have a
    // value_type type member, i.e. the metafunction returns a type only
    // if all types of the members in the std::tuple have a value_type
    // type member, and a std::tuple can be constructed from these:
    using type = std::tuple<typename Ts::value_type...>;
};

template <typename TupOfCtrs>
void doStuff(const TupOfCtrs& tupOfCtrs) {
    using TupOfElements = typename TupOfValueTypes<TupOfCtrs>::type;
    // ...
}
PS:还要注意的是,在许多情况下,如果您只需要一个类型列表,那么
std::tuple
类是一个过度使用的类,可能会稍微影响编译时间。就我个人而言,我总是使用一个简单的
TypeList
struct:

template <typename ... Ts> struct TypeList
{ using type = TypeList<Ts...>; };
模板结构类型列表
{使用type=TypeList;};

如果希望
doStuff
获取
std::tuple
,请明确:

template <class... Ts>
void doStuff(std::tuple<Ts...> const& tupOfCtr) { ... }