The following simple code shows that static_cast to r-value reference type and std::move may change their input argument (here variable inupt) in an initialization statement depending on the type of the object getting initialized. Can someone explain this behavior?
I am at least glad that static_cast and std::move behave similarly since std::move does use static_cast under the hood
#include <iostream>
#include <string>
int main() {
{
std::string input("hello");
std::string&& result = static_cast<std::string&&>(input);
std::cout << "cast case 1: input: " << input << " result: " << result << std::endl; // prints: cast case 1: input: hello result: hello
}
{
std::string input("hello");
std::string result = static_cast<std::string&&>(input);
std::cout << "cast case 2: input: " << input << " result: " << result << std::endl; // prints: cast case 2: input: result: hello
}
{
std::string input("hello");
static_cast<std::string&&>(input);
std::cout << "cast case 3: input: " << input << std::endl; // prints: cast case 3: input: hello
}
{
std::string input("hello");
std::string&& result = std::move(input);
std::cout << "move case 1: input: " << input << " result: " << result << std::endl;
// prints: move case 1: input: hello result: hello
}
{
std::string input("hello");
std::string result = std::move(input);
std::cout << "move case 2: input: " << input << " result: " << result << std::endl;
// prints: move case 2: input: result: hello
}
{
std::string input("hello");
std::move(input);
std::cout << "move case 3: input: " << input << std::endl;
// prints: move case 3: input: hello
}
}