I\'m trying to wrap my head around the new idioms for C++11.
It seems that with shared_ptr at least, there is a substantive difference between using new T()
Correct, reset(new T...)
suffers all the issues of shared_ptr(new T...)
; it will result in double allocation and also is non-exception-safe (there's not much chance of a leak, unless bad_alloc
happens within reset
).
reset
is documented as equivalent to shared_ptr
, so you could also write:
make_shared(args...).swap(mysharedptr);
Assignment from make_shared
is almost equivalent, the only difference being the relative order of the deletion of the old T
and the destruction of the temporary shared_ptr
, which is not observable.