
On 29/04/2010 20:35, Peter Dimov wrote:
Jeffrey Hellrung: ...
I'm confused. So static_cast< Arg&& >(r) creates a new temporary object (rather than just a (rvalue) reference to the r object) when used in direct construction, but does not behave this way otherwise (e.g., the line after "//Correct binding" above? ... Maybe I don't know rvalue references as well as I had thought... :/
I don't think that casting a T (rvalue or lvalue) to T&& was ever supposed to create a temporary; it's always a direct reference binding. This is not a rrv1 vs rrv2 issue; it looks like a plain compiler bug.
Compiles fine and prints "6" in GCC 4.5.0 Best, Ion #include <iostream> //store_and_forward_func takes a Target type, catches all references, //packs them in store_and_forward class, and calls a deferred call //using the stored arguments: class int_holder { int _i; public: int_holder(const int &i) { _i = i; } int_holder(int &&i) { _i = i; i = 0; std::cout << _i << std::endl; } void func(){} }; template<class Target, class Arg> struct store_and_forward { Arg &&ref; //Stores a reference for further forwarding store_and_forward(Arg &&r) : ref(static_cast<Arg&&>(r)) {} void call() { Target a(static_cast<Arg&&>(ref)); a.func(); } }; template<class Target, class Arg> void store_and_forward_func(Arg &&r) { struct store_and_forward<int_holder, Arg> fwd(static_cast<Arg&&>(r)); fwd.call(); } int main () { //This should lead to a compilation error const int & cref = 1; //Ok store_and_forward_func<int_holder>(cref); //Bad forwarding, member reference is not bound //to the temporary int(6) store_and_forward_func<int_holder>(int(6)); return 0; }