Original Post
Consider this code:
void f(int &&value)
{ std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl; }
void f(const int &value)
{ std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl; }
void g(int &&value)
{
std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl;
f(value);
}
void g(const int &value)
{
std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl;
f(value);
}
int main(int argc, char *argv[])
{
std::cout << "Temporary:" << std::endl;
g(123);
std::cout << "\nNon-temporary:" << std::endl;
int value = 123;
g(value);
return 0;
}
It outputs the following:
Temporary:
Called: void g(int&&)
Called: void f([color=#ff0000]const int&) [color=#008080]//I was expecting f(int&&) to be called.
Non-temporary:
Called: void g(const int&)
Called: void f(const int&)
Why isn't the rvalue reference passed to the f() overload taking an rvalue reference?
I can force it to with:
void g(int &&value)
{
f(std::move(value));
}
...but I assumed it'd be propagated automatically.
void f(int &&value)
{ std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl; }
void f(const int &value)
{ std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl; }
void g(int &&value)
{
std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl;
f(value);
}
void g(const int &value)
{
std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl;
f(value);
}
int main(int argc, char *argv[])
{
std::cout << "Temporary:" << std::endl;
g(123);
std::cout << "\nNon-temporary:" << std::endl;
int value = 123;
g(value);
return 0;
}
It outputs the following:
Temporary:
Called: void g(int&&)
Called: void f([color=#ff0000]const int&) [color=#008080]//I was expecting f(int&&) to be called.
Non-temporary:
Called: void g(const int&)
Called: void f(const int&)
Why isn't the rvalue reference passed to the f() overload taking an rvalue reference?
I can force it to with:
void g(int &&value)
{
f(std::move(value));
}
...but I assumed it'd be propagated automatically.