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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -