My question contains two parts:
Does function
static_cast<Т>(arg)
alter the innards ofarg
? Obviously not, according to such code:float i1 = 11.5; int x = static_cast<int>(i1); std::cout << i1<<std::endl; //11.5 std::cout << x<<std::endl; //11
Why does such code:
std::string s1 = "123"; std::string s2 = std::move(s1); std::cout << s1 << std::endl; //empty std::cout << s2 << std::endl; //123
where
std::move()
is using only astatic_cast
to r-value:template<typename _Tp> constexpr typename std::remove_reference<_Tp>::type&& move(_Tp&& __t) noexcept { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
makes
s1
an empty string?
I guess, it is because of using the move constructor of string after s2 =
. It must wipe the initial string by equating to nullptr
or 0 all of the data in the string object. While std::move()
by itself is only returning rvalue. Is it correct?
I know my question is a duplicate of something like static_cast to r-value references and std::move change their argument in an initialization, but I have not found a clear explanation.