Here is a rookie question. Is the following code safe?
boost::unordered_set<std::string> func()
{
boost::shared_ptr<boost::unordered_set<std::string>> list =
boost::make_shared<boost::unordered_set<std::string>>();
/* Code to populate the shared_ptr to unordered_set goes here and I do
populate my set. */
return *list;
}
What will happen first? Copying/NRVO/moving or destruction of shared_ptr
there by causing memory fault? If unsafe, what are my alternatives?
This happens:
It is safe, but the use of dynamic allocation, and shared pointer seems pointless and the inefficiency of the copy may hurt performance if the set is large.
Since you haven't demonstrated any need for using the pointer, I suggest a simpler alternative:
boost::unordered_set<std::string> list;
/* Code to populate the unordered_set goes here and I do
populate my set. */
return list;
NRVO can be applied to this and if it's not applied, the return value is constructed by move.