lundi 16 août 2021

Pass a std::string ctor parameter to impl in pimpl with move semantics

Is this a correct approach to pass std::string parameters to the Impl's constructor with move semantics?

#include <string>
#include <memory>

using namespace std;

class Foo
{
    struct Impl;
    unique_ptr<Impl> impl_;
public:
    Foo(string s);
};

struct Foo::Impl
{
    string s_;
    Impl(string s) : s_(std::move(s)) {}
};

Foo::Foo(string s) : impl_(make_unique<Foo::Impl>(move(s)))
{
}

Or should the Impl's ctor be defined as:

Impl(string&& s) : s_(std::move(s)) {}

Aucun commentaire:

Enregistrer un commentaire