std :: tuple作为类成员

3 c++ templates stl

如何使用类型成员创建对象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 };
Run Code Online (Sandbox Code Playgroud)

但它无法使用以下错误进行编译.

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

代码有什么问题?

0x4*_*2D2 10

args是可变的,所以你必须扩展它...:

: tuple_(std::make_tuple(args...))
//                           ^^^
Run Code Online (Sandbox Code Playgroud)

你不需要make_tuple这个:

: tuple_(args...)
Run Code Online (Sandbox Code Playgroud)