Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

This is a follow-on question to C++0x rvalue references and temporariesC++0x rvalue references and temporaries

My motivation for asking this is something like the question asked in How to reduce redundant code when adding new c++0x rvalue reference operator overloadsHow to reduce redundant code when adding new c++0x rvalue reference operator overloads ("How to reduce redundant code when adding new c++0x rvalue reference operator overloads").

This is a follow-on question to C++0x rvalue references and temporaries

My motivation for asking this is something like the question asked in How to reduce redundant code when adding new c++0x rvalue reference operator overloads ("How to reduce redundant code when adding new c++0x rvalue reference operator overloads").

This is a follow-on question to C++0x rvalue references and temporaries

My motivation for asking this is something like the question asked in How to reduce redundant code when adding new c++0x rvalue reference operator overloads ("How to reduce redundant code when adding new c++0x rvalue reference operator overloads").

edited tags
Link
Johannes Schaub - litb
  • 509.8k
  • 132
  • 928
  • 1.2k
added 1165 characters in body
Source Link
Doug
  • 9.2k
  • 2
  • 30
  • 38

Clarification/edit: I realize this is virtually identical to accepting arguments by value for movable types, like C++0x std::string and std::vector (save for the number of times the move constructor is conceptually invoked). However, it is not identical for copyable, but non-movable types, which includes all C++03 classes with explicitly-defined copy constructors. Consider this example:

class legacy_string { legacy_string(const legacy_string &); }; //defined in a header somewhere; not modifiable. void f(legacy_string s1, legacy_string s2); //A *new* (C++0x) function that wants to move from its arguments where possible, and avoid copying void g() //A C++0x function as well { legacy_string x(/*initialization*/); legacy_string y(/*initialization*/); f(std::move(x), std::move(y)); } 

If g calls f, then x and y would be copied - I don't see how the compiler can move them. If f were instead declared as taking legacy_string && arguments, it could avoid those copies where the caller explicitly invoked std::move on the arguments. I don't see how these are equivalent.

Clarification/edit: I realize this is virtually identical to accepting arguments by value for movable types, like C++0x std::string and std::vector (save for the number of times the move constructor is conceptually invoked). However, it is not identical for copyable, but non-movable types, which includes all C++03 classes with explicitly-defined copy constructors. Consider this example:

class legacy_string { legacy_string(const legacy_string &); }; //defined in a header somewhere; not modifiable. void f(legacy_string s1, legacy_string s2); //A *new* (C++0x) function that wants to move from its arguments where possible, and avoid copying void g() //A C++0x function as well { legacy_string x(/*initialization*/); legacy_string y(/*initialization*/); f(std::move(x), std::move(y)); } 

If g calls f, then x and y would be copied - I don't see how the compiler can move them. If f were instead declared as taking legacy_string && arguments, it could avoid those copies where the caller explicitly invoked std::move on the arguments. I don't see how these are equivalent.

added 4 characters in body
Source Link
Doug
  • 9.2k
  • 2
  • 30
  • 38
Loading
Source Link
Doug
  • 9.2k
  • 2
  • 30
  • 38
Loading