Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
If you have a class which needs to store a construction parameter internally, and you want to take advantage of move semantics, I understand that the parameter should be passed by value: class Foo...
#1: Initial revision
Do you need to use std::move to store a parameter passed by value?
If you have a class which needs to store a construction parameter internally, and you want to take advantage of move semantics, I understand that the parameter should be passed by value: class Foo { std::string _string; public: Foo(std::string s): _string(s) {} }; ... Foo foo(std::string("Temporary value")); // rvalue is moved in However I'm a little unclear on the code required inside the constructor. Do you need to use `std::move` to preserve the rvalue reference, e.g. Foo(std::string s): _string(std::move(s)) {} or does the compiler treat the parameter `s` as a moveable value by default (since it's going to be destroyed at the end of the function body), rendering the `std::move` unnecessary: Foo(std::string s): _string(s) {}