Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

Post History

71%
+3 −0
#1: Initial revision by user avatar InfiniteDissent‭ · 2022-07-13T09:09:31Z (3 months ago)
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) {}