c++ - C++11 constructor argument: std::move and value or std::forward and rvalue reference -
which of below 2 should preferred , why?
struct x { y data_; explicit x(y&& data): data_(std::forward<y>(data)) {} }; vs
struct x { y data_; explicit x(y data): data_(std::move(data)) {} };
the 2 variants differ in functionality. following statements work second one–but not first one:
y y; x x(y); if looking same functionality, 2 variants should follows:
struct x { y data_; explicit x(const y& data) : data_(data) { } explicit x(y&& data) : data_(std::move(data)) { } }; struct x { y data_; explicit x(y data) : data_(std::move(data)) { } }; the first variant saves 1 move operation, whereas second variant less write. so, answer is: use latter long have no reason optimize performance.
Comments
Post a Comment