C++ 0x可变参数模板通过引用传递

usm*_*man 33 c++ templates c++11

我想为我的应用程序使用可变参数模板功能,但我不希望通过值传递对象(因为对象在我的情况下非常复杂).我想通过引用传递它们(而不是作为指针).

void func()
{
}

template<typename Function1, typename... FurtherFunctions>
void func(Function1 f1, FurtherFunctions... further_functions)
{
    // doing some processing here...
}
Run Code Online (Sandbox Code Playgroud)

我如何通过引用传递参数并确保它们不被复制?

Ker*_* SB 37

说啊:

template <typename ...Args>
int myfunction(Args & ... args)
{
  /* ... */
}
Run Code Online (Sandbox Code Playgroud)

同样的const Args & ... args,Args && ... args等等

  • 实际上`&&`在这里有意义,因为你可能想要使用`std :: forward`. (4认同)
  • `Args &&`是一个右值引用,您永远不需要在自己的代码中编写它.我只是提到它,因为它存在,你偶尔可以[看到它](http://stackoverflow.com/questions/6114067/how-to-emulate-c-array-initialization-int-arr-e1-e2- E3行为/ 6272491#6272491). (2认同)