Created
March 6, 2023 21:47
-
-
Save blockspacer/2df0d6f8036b192b18e40de8e0d674ba to your computer and use it in GitHub Desktop.
Advantages of pass-by-value and std::move over pass-by-reference https://stackoverflow.com/questions/51705967/advantages-of-pass-by-value-and-stdmove-over-pass-by-reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/22334896
f you are looking for the same functionality, the two variants should look as follows:
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.