4

考虑代码

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:

为什么不将文字推导出为右值?

4

1 回答 1

7

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

foo (std::forward<Args>(args)...);
于 2012-11-09T23:07:27.447 回答