The C++ primer
shows an example:
auto &nos = result[word];
if (!nos) nos.reset(new std::set<int>);
Where result is std::map<string, shared_ptr<std::set<int>>>
.
My question is: if !nos
holds, is nos
null? then why can we call reset
method on this null shared_ptr
?
Yes, it's null, in the it doesn't point to any data sense, but it's still a valid shared_ptr
object. In the example code, you re-set it with a valid pointer.
Alternatively, you could write this:
auto &nos = result[word];
if (!nos) nos = std::make_shared<std::set<int>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With