1. 程式人生 > >C++11 move的實現

C++11 move的實現

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實現,並無神祕之處。