
On Thu, Feb 16, 2012 at 11:30 PM, Jeffrey Lee Hellrung, Jr. <jeffrey.hellrung@gmail.com> wrote:
On Thu, Feb 16, 2012 at 3:06 PM, Robert Dailey <rcdailey@gmail.com> wrote:
On Thu, Feb 16, 2012 at 5:01 PM, Jeffrey Lee Hellrung, Jr. < jeffrey.hellrung@gmail.com> wrote:
Well I think you can always make NRVO apply if you're always returning
the
same variable, which is always possible: just swap what you would otherwise return into that common variable.
In general, though, no, I don't think you can cleanly introduce (emulated) move semantics non-intrusively in C++03.
Just to be clear, what "common variable" are you referring to?
Thanks for your answers.
std::string foo() { std::string result; /*...*/ // would like to "return x;", but instead... result.swap(x); return result; /*...*/ // would like to "return y;", but instead... result.swap(y); return result; }
I think this is the simplest "move emulation" in C++03, which simply encapsulates the pattern above: template<class Swappable> Swappable move(Swappable& x) { Swappable result; // Also requires DefaultConstructible using std::swap; swap(x, result); return result; // NRVO } Usage: std::string foo() { std::string result; /*...*/ // would like to "return x;", but instead... return move(x); /*...*/ // would like to "return y;", but instead... return move(y); } Note that it is sematically different from std::move as it will always modify the argument, while std::move is similar to a cast to rvalue reference. HTH, -- gpd