
On 5/25/2010 1:49 PM, Ion GaztaƱaga wrote:
On 25/05/2010 18:06, Jeffrey Lee Hellrung, Jr. wrote:
We could provide a macro that translates rvalues into emulated rvalue references (if movable) or const references (if not movable). Unfortunately, this kills by-value parameter optimizations in C++03 :/
Sorry, but I don't get this, could you elaborate a bit more?
If you're writing a generic algorithm that invokes a function (perhaps one given to the algorithm as a parameter) which happens to return an rvalue, and you want to ensure that this rvalue gets captured as an rvalue reference, you can wrap the function call in a macro that will ensure such a translation takes place in C++03. That is, BOOST_MOVE_RVALUE( Expr ) -> lvalue (if Expr is an lvalue) BOOST_MOVE_RVALUE( Expr ) -> lvalue-to-const (if Expr is a non-movable rvalue in C++03) BOOST_MOVE_RVALUE( Expr ) -> emulated rvalue reference (if Expr is a movable rvalue in C++03) Of course, in C++0x, BOOST_MOVE_RVALUE( Expr ) should just expand to Expr (or maybe "( Expr )", without quotes). Perhaps a little more concretely: template< ..., class F, ... > void my_generic_algorithm( ..., F f, ... ) { typedef ... T; .... T x = ... ; ... //x = f(...); // will this result in an optimal move assignment in C++03, if f(...) returns an rvalue? x = BOOST_MOVE_RVALUE( f(...) ); // explicit move assignment used, even in C++03 ... } In retrospect, perhaps I didn't understand the original concern correctly. This is likely more applicable when you want to forward the result of f(...) to another function that captures its parameters by BOOST_FWD_REF, for example. Let me know if this makes any sense... - Jeff