<分区>
下面是代码。为什么如果我替换 typename remove_reference<S>::type&
与 S&
它不会很好地工作? (我的意思是一个类型会被推错)
如果我传递一个右值(让 int 为 int),那么 S
将被推断为 int
, a
的类型是 int&
, forward()
返回 int&&
(右值)。
如果我传递一个左值 ( int
) S
将被推断为 int&
, a
的类型是 int& & ->int&
, forward()
返回 int& &&->int&
.一切正常,那么我们为什么需要那个 remove_reference
?
template<class S>
S&& forward(typename remove_reference<S>::type& a) noexcept
{
return static_cast<S&&>(a);
}