Created
April 3, 2019 12:48
-
-
Save dc3671/2c7a0ea56f7284e3d33ba0b78e8f8b0f to your computer and use it in GitHub Desktop.
test c++1z move/forward implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
template <typename _Tp> | |
inline typename std::remove_reference<_Tp>::type&& my_move(_Tp&& __t) { | |
cout << &__t << endl; | |
return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); | |
} | |
template <typename _Tp> | |
inline _Tp&& my_forward(typename std::remove_reference<_Tp>::type& __t) { | |
cout << "calling lvalue forward" << endl; | |
return static_cast<_Tp&&>(__t); | |
} | |
template <typename _Tp> | |
inline _Tp&& my_forward(typename std::remove_reference<_Tp>::type&& __t) { | |
cout << "calling rvalue forward" << endl; | |
static_assert(!std::is_lvalue_reference<_Tp>::value, | |
"template argument" | |
" substituting _Tp is an lvalue reference type"); | |
return static_cast<_Tp&&>(__t); | |
} | |
struct X {}; | |
void inner(const X&) { cout << "inner(const X&)" << endl; } | |
void inner(X&&) { cout << "inner(X&&)" << endl; } | |
template <typename T> | |
void outer(T&& t) { | |
inner(my_forward<T>(t)); | |
} | |
int main() { | |
cout << ">>> test move" << endl; | |
X a1; | |
cout << &a1 << endl; | |
cout << "> test lvalue" << endl; | |
X&& b = my_move(a1); | |
cout << "> test rvalue" << endl; | |
X&& c = my_move(X()); | |
cout << endl; | |
cout << ">>> test forward" << endl; | |
X a2; | |
cout << "> test lvalue indirectly" << endl; | |
outer(a2); | |
cout << "> test rvalue indirectly" << endl; | |
outer(X()); | |
cout << "> test rvalue directly" << endl; | |
inner(my_forward<X>(X())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment