【问题标题】:std::tuple as class memberstd::tuple 作为类成员
【发布时间】:2013-10-27 17:05:12
【问题描述】:

如何创建具有std::tuple 类型成员的对象?

我试图编译这段代码。

  6 template <class ... T>
  7 class Iterator
  8 {
  9 public:
 10         Iterator(T ... args)
 11                 : tuple_(std::make_tuple(args))
 12         {
 13         }
 14 
 15 private:
 16         std::tuple<T ...> tuple_;
 17 };

但无法编译,出现以下错误。

variadic.cpp: In constructor ‘Iterator<T>::Iterator(T ...)’:
variadic.cpp:11:33: error: parameter packs not expanded with ‘...’:
variadic.cpp:11:33: note:         ‘args’

代码有什么问题?

【问题讨论】:

    标签: c++ templates stl


    【解决方案1】:

    args 是可变参数,所以你必须用... 扩展它:

    : tuple_(std::make_tuple(args...))
    //                           ^^^
    

    你不需要make_tuple

    : tuple_(args...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多