Re: Does std::unique_ptr support self-reset?
On Sunday, August 11, 2013 6:10:02 PM UTC-4, Daniel Kr?gler wrote:
At least in VS2010 and 2012, unique_ptr::reset checks for setting
the same pointer and ignores it.
This is indeed a non-conforming C++11 implementation, because
those effects were intentionally changed as of
I guess the libstdc++ implementation is non-conforming too.
void
reset(pointer __p = pointer()) noexcept
{
using std::swap;
swap(std::get<0>(_M_t), __p);
if (__p != pointer())
get_deleter()(__p);
}
(though the array specialization matches the standard)
http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/include/bits/unique_ptr.h?view=markup
I read the defect report, but I still can't understand why someone would
explicitly change from specifying that p.reset(p.get()) was safe to it
resulting in undefined behavior (dangling pointer immediately and then a
double-delete in the unique_ptr destructor), especially for a class whose
entire purpose is to help eliminate memory errors such as dangling pointers
and double-frees. This is especially puzzling to me when I then read 998,
where the standard was changed to eliminate UB in a case that seems to me
to be even more likely to be a coding error (my bad_idea example above).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]