【发布时间】:2013-07-15 19:44:46
【问题描述】:
为什么引用折叠不适用于
template<typename T, template<typename> class C>
void f(C<T> && x); // x declaration is an rvalue!
如何完善前向以及如何避免 const lvalue ref、lvalue ref、rvalue ref in 的所有组合重载
template<typename T> // not necessary a template template class here
void f(C<T>, C<T>, C<T>, ..., C<T>)
{
// do something with T
// move or copy arguments to a function
}
【问题讨论】:
-
引用折叠仅在表单为
T&&时适用。C<T>&&总是指定一个右值引用(如std::vector<T>&&) -
@AndyProwl:或者当表单是
auto &&。 -
@a.lasram:如果您需要完美转发,请将表单设为
template<typename U> f(U&&)并添加一个 SFINAE 约束,说明对于某些T,U必须是C<T>的实例化。 -
@a.lasram:因为我必须详细说明它才能使它成为一个体面的答案,而且我现在没有时间:(
-
这真是一团糟。该死的 C++11。
标签: c++ templates c++11 rvalue-reference