Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/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++11 将参数包存储为元组引用_C++11_Tuples_Variadic Templates_Variadic Functions_Perfect Forwarding - Fatal编程技术网

C++11 将参数包存储为元组引用

C++11 将参数包存储为元组引用,c++11,tuples,variadic-templates,variadic-functions,perfect-forwarding,C++11,Tuples,Variadic Templates,Variadic Functions,Perfect Forwarding,我试图存储可变模板的左值引用的参数包,以供以后使用 我现在有以下工作 template <typename... Ts> class Foo { private: std::tuple<Ts...> m_args; public: template<typename... Args> Foo(Args&&... args) : m_args(std::make_tuple(std::forward&l

我试图存储可变模板的左值引用的参数包,以供以后使用

我现在有以下工作

template <typename... Ts>
class Foo {
private:
        std::tuple<Ts...> m_args;
public:
       template<typename... Args>
       Foo(Args&&... args) : m_args(std::make_tuple(std::forward<Args>(args)...))
       {
       }
 };

 int main() {
     int x = 10;
     int y = 20;
     Foo<int, int> foo(x, y);
 }
模板
福班{
私人:
std::元组m_参数;
公众:
模板
Foo(Args&&…Args):m_Args(std::make_tuple(std::forward(Args)…)
{
}
};
int main(){
int x=10;
int y=20;
富富(x,y);
}
但是,我希望将参数包存储为引用,以便以后可以访问相同的对象。
我不知道我怎样才能做到这一点。任何帮助都将不胜感激。

我能想到的最好的方法是使用
std::forward\u as\u tuple

不幸的是,我看不到一种将其用于完美转发的方法:如果您希望在类中的元组中注册值,则必须一次性确定元组的类型

我能想象的最好的情况是常量引用的元组;如下

template <typename ... Ts>
class Foo
 {
   private:
      std::tuple<Ts const & ...> m_args;

   public:
      Foo (Ts const & ... as) : m_args{std::forward_as_tuple(as...)}
       { }
 };
模板
福班
{
私人:
std::元组m_参数;
公众:
Foo(Ts const&…as):m_args{std::forward_as_tuple(as…)
{ }
};

我希望没有必要记住,悬空引用对于基于引用元组的解决方案来说是多么危险。

类型是已知的,只有参数的数量会发生变化。@ZincFur-你的意思是所有的
Ts..
类型都是相同的类型吗?那么,为什么要使用
std::tuple
而不是
std::array
?我不希望动态初始化,也不希望将参数的数量指定为模板参数。仍然可以使用std::array来存储参数包吗?@ZincFur-使用SFINAE来强制规定
Args的数量…
typenames正好是
std::array
的大小如何?而且,是的,您可以使用
std::array
存储参数包(如果
m_args
std::array
,您可以编写
Foo(args const&…as):m_args{{as…}
),但不幸的是,您无法生成引用数组。