I know std::move
A const Object will actually invoke T
's copy constructor,
So I want to do some Experiments of implementation of my move and inner of this remove to remove const
such like :
template<typename _Tp>
typename std::remove_const<typename std::remove_reference<_Tp>::type>::type&&
my_move(_Tp&& __t) noexcept {
using removed_reference = typename std::remove_reference<_Tp>::type;
using removed_ref_const = typename std::remove_const<removed_reference>::type;
return static_cast<removed_ref_const&&>(__t);
}
but This code will not compile? Why
And If I change the order of remove_reference
and remove_const
, This code will compile but not as my expect, my_move(const Object T)
still uses Object T
's copy constructor?
And Also who can give me a right implementation which will show when I remove const
, This will use T
's move constructor.
T may be as:
struct T
{
T() = default;
T(const T&) { std::cout << "copy ctor\n"; }
T(T&&) { std::cout << "move ctor\n"; }
};
int main() {
const T t;
T a = my_move(t);
}