Just to see how std::move works, I tried to mimic (well copied original code)the std::move function; but to my surprise it does not work as std::move. My move function ends-up calling copy ctor instead of move ctor.
#include <iostream>
#define My_version
template <class _Ty>
struct remove_reference {
using type = _Ty;
using _Const_thru_ref_type = const _Ty;
};
template <class _Ty>
using remove_reference_t = typename remove_reference<_Ty>::type;
#ifdef My_version
template<typename T> // in namespace std
typename remove_reference<T>::type&&
mymove(T&& param)
{
std::cout << "My_version\n";
using ReturnType = typename remove_reference<T>::type&&;
return static_cast<ReturnType>(param);
}
#else
template <class _Ty>
constexpr remove_reference_t<_Ty>&& mymove(_Ty&& _Arg) noexcept { // forward _Arg as movable
return static_cast<remove_reference_t<_Ty>&&>(_Arg);
}
#endif // My_version
class simple
{
public:
simple(int kk) :k(kk) {}
simple(simple&& rhs) noexcept
{
std::cout << "move is called \n";
k = rhs.k;
rhs.k = -1; //moved
}
simple(const simple& rhs)noexcept
{
std::cout << "copy is called \n";
k = rhs.k*10;
}
int k;
};
int main()
{
{
std::cout << "===============================\nmymove\n===============================\n";
simple a(42);
simple b = mymove(a);
std::cout << " after move\nmovedObj.k " << b.k << " originalObj.k " << a.k << "\n";
}
{
std::cout << "\n\n===============================\nstd::move\n===============================\n";
simple a(42);
simple b = std::move(a);
std::cout << "after move\nmovedObj.k " << b.k << " originalObj.k " << a.k << "\n";
}
return 0;
}
I tried both exact copy of std::move function and slightly different style function as can be seen here separated by #ifdef. Unlike std::move both call copy ctor.
std::move causes move constructor why my move function causes copy.
===============================
mymove
===============================
copy is called
after move
movedObj.k 420 originalObj.k 42
===============================
std::move
===============================
move is called
after move
movedObj.k 42 originalObj.k -1
As suggested by - HolyBlackCat, Adding two more reference specialisations for remove_reference fixed the problem.