C++11 move的实现

版权声明:版权所有,转载请注明出处 https://blog.csdn.net/songchuwang1868/article/details/83506841
template <class T>
typename remove_reference<T>::type&& move(T&& t) //通过trait技法推断出返回值。参数类型是T&&万能引用,所以move不仅可以把左值转成右值,也可以把右值转成右值
{
    using RRefType = typename remove_reference<T>::type&&;//取别名RRefType 
    return static_cast<RRefType>(t);//由此可见直接可以通过静态强转成右值
}

1、通过trait技法推断出返回值。

2、参数类型是T&&万能引用,所以move不仅可以把左值转成右值,也可以把右值转成右值。

3、通过using取别名RRefType

4、左值转成右值直接通过static_cast实现,并无神秘之处。

猜你喜欢

转载自blog.csdn.net/songchuwang1868/article/details/83506841