Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

in C++, is std::move still preferred when calling a function that takes in a const reference?

Of the two versions of function calls below, is the one with std::move still preferred?

void myFunc(const std::string& myStr){
 //
}

std::string MyStr = "my string";

//For these 2 versions, should I still prefer std::move here to save a value copy, even when the function itself takes in a reference?
myFunc(std::move(MyStr));
myFunc(MyStr);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When passing a value by reference std::move doesn’t make any sense, because no instantiation is happening here, and there would be no side effects (provided you don’t want to alter overload function candidate)

Thus for this particular case there is no any difference and you don’t need std::move

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading