rvalue references - Why aren't they forwarded?
#1 Marketplace Seller - Reputation: 8908
Posted 07 November 2012 - 02:04 PM
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(const int&) //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.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#3 Marketplace Seller - Reputation: 8908
Posted 07 November 2012 - 02:19 PM
Edited by Servant of the Lord, 07 November 2012 - 02:21 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#4 Moderators - Reputation: 6621
Posted 07 November 2012 - 02:20 PM
#5 Members - Reputation: 203
Posted 07 November 2012 - 03:42 PM
If you call a function or assign something and want to move the value instead of copying it, you do something like someFunction( move( thing ) ) or anotherthing = move( thing ). This way you know that thing is not valid anymore after that.
(btw I don't remember what the standard says about the state of an object after it has been moved, iirc it's just up to the move constructor or move operator to make sure the object is in a state where its destructor won't blow up once it goes out of scope)
Edited by Zlodo, 07 November 2012 - 03:43 PM.
#6 Moderators - Reputation: 4632
Posted 07 November 2012 - 04:50 PM
Nothing happens to the object itself when it is moved. It is not the object that is moved, but it is the object's resources that have changed owners. So it is up to the move constructors/operators to ensure that the resources are transferred properly and that the object on the right hand side is still valid, although empty since its resources have been transferred to another object.(btw I don't remember what the standard says about the state of an object after it has been moved, iirc it's just up to the move constructor or move operator to make sure the object is in a state where its destructor won't blow up once it goes out of scope)
Edited by Brother Bob, 07 November 2012 - 04:51 PM.
#7 Moderators - Reputation: 6621
Posted 07 November 2012 - 05:51 PM
#8 Members - Reputation: 681
Posted 07 November 2012 - 08:07 PM
{
std::cout << "Called: " << __PRETTY_FUNCTION__ << std::endl;
f(std::forward(value));
}
I never used perfect forward before, but I think you need explicitly cast value to a rvalue, like what std::forward does.
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.






