C++ 如何从包装在元组中的数据构造对象?

C++ 如何从包装在元组中的数据构造对象?,c++,c++11,constructor,tuples,variadic-templates,C++,C++11,Constructor,Tuples,Variadic Templates,如果我有 struct SomeType { template<typename... Args> SomeType(Args... args); // ... }; template<typename... Args> std::tuple<Args> data; // syntax correct? 编辑1我认为问题很清楚,但凯文不同意。。。我想调用someone的构造函数,将元组中的值打包为参数。明白了吗,凯文 编辑2我以为一个问

如果我有

struct SomeType {
  template<typename... Args>
  SomeType(Args... args);
  // ...
};

template<typename... Args> std::tuple<Args> data;   // syntax correct?

编辑1我认为问题很清楚,但凯文不同意。。。我想调用
someone
的构造函数,将
元组中的值打包为参数。明白了吗,凯文


编辑2我以为一个问题就是一个问题,但凯文又不同意。为了他的利益,这里有一个用例:我想动态构造线程本地对象,每个对象都是由它的线程根据前面提供的一些参数构造的。考虑

template<typename Object>
class thread_specific
{
public:
  // default constructor: objects will be default constructed
  thread_specific();
  // objects will be copy constructed from specimen provided
  thread_specific(Object const&specimen);
  // objects will be constructed from arguments provided
  template<typename... Args>
  thread_specifiv(Args... args);
  // return thread-specific object; objects are constructed only when needed
  Object& local_object();
};
模板
特定于线程的类
{
公众:
//默认构造函数:将默认构造对象
线程特定();
//对象将由提供的样本复制而成
特定螺纹(对象常数和试样);
//对象将根据提供的参数构造
样板
螺纹规格(参数…参数);
//返回特定于线程的对象;仅在需要时构造对象
对象和局部对象();
};

(例如,通过
std::map
实现)。现在,如果用户使用第三个构造函数创建了一个全局
thread\u specific
对象,则必须以某种方式存储参数,并在需要时将其提供给
object
的构造函数,即在第一次调用
thread\u specific::local\u object()时
在每个
std::thread
上使用
index\u sequence
解压
std::tuple
(或
std::pair
std::array
,或任何其他支持tuple接口的内容):


例如,在C++11模式下:

使用
索引\u序列
解包
std::tuple
(或
std::pair
std::array
,或任何其他支持tuple接口的内容):


例如,在C++11模式下:

要复制元组吗?相同的东西,不同的名字?你想要一个typedef,还是你想要做什么?知道如何使用它可能会有所帮助。相关:或者@NateKohl是的,我看到了。没有公认的答案。排名最高的答案使用一些冗长的递归。我想知道是否可以更快地完成,或者标准是否提供了一些支持。是否要复制元组?相同的东西,不同的名字?你想要一个typedef,还是你想要做什么?知道如何使用它可能会有所帮助。相关:或者@NateKohl是的,我看到了。没有公认的答案。排名最高的答案使用一些冗长的递归。我想知道是否可以更快地完成,以及/或者该标准是否提供了一些支持。
template<typename Object>
class thread_specific
{
public:
  // default constructor: objects will be default constructed
  thread_specific();
  // objects will be copy constructed from specimen provided
  thread_specific(Object const&specimen);
  // objects will be constructed from arguments provided
  template<typename... Args>
  thread_specifiv(Args... args);
  // return thread-specific object; objects are constructed only when needed
  Object& local_object();
};
#include <utility>
#include <tuple>

template <typename Tuple, std::size_t... Inds>
SomeClass help_make_SomeClass(Tuple&& tuple, std::index_sequence<Inds...>)
{
    return SomeClass(std::get<Inds>(std::forward<Tuple>(tuple))...);
}

template <typename Tuple>
SomeClass make_SomeClass(Tuple&& tuple)
{
    return help_make_SomeClass(std::forward<Tuple>(tuple),
        std::make_index_sequence<std::tuple_size<Tuple>::value>());
}
template <std::size_t... Inds>
struct index_sequence {
    static constexpr std::size_t size()
    { return sizeof...(Inds); }
};

template <std::size_t N, std::size_t... Inds>
struct help_index_seq {
    typedef typename help_index_seq<N-1, N-1, Inds...>::type type;
};

template <std::size_t... Inds>
struct help_index_seq<0, Inds...> {
    typedef index_sequence<Inds...> type;
};

template <std::size_t N>
using make_index_sequence = typename help_index_seq<N>::type;