【问题标题】:rvalue reference and literal右值引用和文字
【发布时间】:2012-11-09 23:03:20
【问题描述】:

考虑代码

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:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2020-01-03
      • 2016-04-24
      • 1970-01-01
      • 2018-02-10
      相关资源
      最近更新 更多