I see in the highly-upvoted answer here (https://stackoverflow.com/a/37885232/3754760) there are two ways to convert a unique_ptr to a shared_ptr, namely by creating the unique_ptr first and then move
-ing it to the shared_ptr, as well as assigning the unique_ptr directly to the shared_ptr.
Example:
std::unique_ptr<std::string> unique = std::make_unique<std::string>("test");
std::shared_ptr<std::string> shared = std::move(unique);
or:
std::shared_ptr<std::string> shared = std::make_unique<std::string>("test");
Are the two above equivalent, performance-wise? Secondly, I see a warning like Moving a temporary object prevents copy elision
when doing something like this:
std::shared_ptr<std::string> shared = std::move(std::make_unique<std::string>("test"));
As someone pretty new to smart pointers, could someone explain what this warning means and why it occurs in the 3rd example? Thanks!
Aucun commentaire:
Enregistrer un commentaire