右值参考和文字

4 c++ move forward rvalue c++11

考虑一下代码

template <typename... Args>
void foo (Args&& ...)
{
}

template <typename... Args>
void bar (Args&& ... args)
{
   foo (std::forward (args)...);
}

int main ()
{
  bar (true);
}
~                   
Run Code Online (Sandbox Code Playgroud)

gcc 4.7.2给出了错误

error: no matching function for call to ‘forward(bool&)’
note: candidates are:

template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_Tp>::type&)
note:   template argument deduction/substitution failed:

note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_Tp>::type&&)
note:   template argument deduction/substitution failed:
Run Code Online (Sandbox Code Playgroud)

为什么不将文字推断为右值?

Die*_*ühl 7

您没有std::forward()正确使用:您需要将推导出的类型作为参数std::forward():

foo (std::forward<Args>(args)...);
Run Code Online (Sandbox Code Playgroud)