Skip to content

Instantly share code, notes, and snippets.

@blockspacer
Created March 6, 2023 21:47
Show Gist options
  • Save blockspacer/2df0d6f8036b192b18e40de8e0d674ba to your computer and use it in GitHub Desktop.
Save blockspacer/2df0d6f8036b192b18e40de8e0d674ba to your computer and use it in GitHub Desktop.
struct XYZ {
XYZ(std::unique_ptr<std::string> str) : m_str (std::move(str)){}
std::unique_ptr<std::string> m_str;
};
XYZ xyz(std::make_unique<std::string>("123"));
BOOST_CHECK_EQUAL(*(xyz.m_str), "fggfgf");
@blockspacer
Copy link
Author

https://stackoverflow.com/a/22334896

f you are looking for the same functionality, the two variants should look as 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 one move operation, whereas the second variant is less to write. So, the answer is: Use the latter as long as you have no reason to optimize the performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment