右值参考和文字

时间:2012-11-09 23:03:20

标签: c++ c++11 move forward rvalue

考虑代码

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

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

int main ()
{
  bar (true);
}
~                   

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:

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

1 个答案:

答案 0 :(得分:7)

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

foo (std::forward<Args>(args)...);