Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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

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++ 如何为可变模板子对象生成元组?_C++_Templates_C++17_Variadic - Fatal编程技术网

C++ 如何为可变模板子对象生成元组?

C++ 如何为可变模板子对象生成元组?,c++,templates,c++17,variadic,C++,Templates,C++17,Variadic,简而言之:如何从给定类型的子类型创建变量元组 在下面的代码中,get_children调用中存在错误的children类型。目标是接收从给定元素派生的具有子元素的元组 template<typename T> class Elem{ public: T e; class Child{ public: T c; }; Child child() { return Child{ .c = e}; }; }; template <typename... T

简而言之:如何从给定类型的子类型创建变量元组

在下面的代码中,get_children调用中存在错误的children类型。目标是接收从给定元素派生的具有子元素的元组

template<typename T>
class Elem{
public:
    T e;
    class Child{ public: T c; };

    Child child() { return Child{ .c = e}; };
};

template <typename... T>
class Collection{
public:
    using arr = std::tuple<T...>;
    arr elems;

    using children = std::tuple<T...::Child>;  /// **** HERE IS ERROR ***

    auto get_children(){
        children res;

        get_child<sizeof...(T)-1>( res );
        return res;
    }

private:
    template< size_t Num >
    auto get_child( children &res ){
        std::get<Num>(res) = std::get<Num>(elems).child();
        get_child<Num-1>(res);
    }

    template<>
    auto get_child<0>( children &res ){
        std::get<0>(res) = std::get<0>(elems).child();
    }
};
模板
类元素{
公众:
T e;
类子{public:tc;};
Child(){return Child{.c=e};};
};
样板
类集合{
公众:
使用arr=std::tuple;
arr元素;
使用children=std::tuple;///****这是一个错误***
自动获取子对象(){
儿童;
获得儿童(res);
返回res;
}
私人:
模板
自动获取子对象(子对象和资源){
std::get(res)=std::get(elems).child();
获得儿童(res);
}
样板
自动获取子对象(子对象和资源){
std::get(res)=std::get(elems).child();
}
};

提前谢谢

因为您标记了c++17,所以请使用
apply

#include <iostream>
#include <tuple>
#include <boost/type_index.hpp>

template<typename T>
class Elem{
public:
    T e;
    class Child{ public: T c; };

    Child child() { return Child{ .c = e}; };
};

template <typename... T>
class Collection{
public:

    using arr = std::tuple<T...>;
    arr elems;

    auto get_children(){
        return std::apply( [](auto&&... e){ return std::make_tuple(  e.child()... ); } , elems);
    }

private:
};

int main()
{
    Collection<Elem<int>,Elem<char>,Elem<float>> c;
    auto t = c.get_children();
    std::cout << boost::typeindex::type_id_with_cvr<decltype(t)>().pretty_name() << std::endl;
     // std::tuple<Elem<int>::Child, Elem<char>::Child, Elem<float>::Child>
}
#包括
#包括
#包括
样板
类元素{
公众:
T e;
类子{public:tc;};
Child(){return Child{.c=e};};
};
样板
类集合{
公众:
使用arr=std::tuple;
arr元素;
自动获取子对象(){
return std::apply([](auto&&…e){return std::make_tuple(e.child()…);},elems);
}
私人:
};
int main()
{
c组;
自动t=c。获取子项();
std::cout那怎么办

using children = std::tuple<typename T::Child...>;
使用children=std::tuple;
?

我的意思是…单个类型是
T::Child
因此,要重复这些类型,您必须在类型之后放置省略号(
),因此
T::Child…


但是,给定一个泛型类型
T
T::Child
既可以是一个类型,也可以是一个成员或一个方法。因此,您需要在
typename
之前通知编译器,以下
T::Child
是一个类型,而不是一个成员或其他。

在您的设计中有一些严重的问题……这是一个理论问题。实际上,Child将是另一个非模板类。请注意,包是
T
,而不是
T..
-省略号不是其名称的一部分,而是用于扩展类型名称或用其表示的表达式。