When implementing my own unique_ptr
( just for fun), I found it cannot pass this test file from libstdcxx
:
struct A;
struct B
{
std::unique_ptr<A> a;
};
struct A
{
B* b;
~A() { VERIFY(b->a != nullptr); }
};
void test01()
{
B b;
b.a.reset(new A);
b.a->b = &b;
}
gcc passes this test file happily (of course, this file is from libstdcxx), while clang fails for the VERIFY
part.
Question:
- Is it implementation dependent or undefined behavior?
- I guess this postcondition (
b->a != nullptr
) is important for gcc, otherwise it'll not have a test file for it, but I don't know what's behind it. Is it related to optimization? I know many UB are for better optimizations.
unique_ptr
implementation for reference (at least the destructor). – user673679unique_ptr
(see the Wandbox links). – Holtclang
on coliru uses libstdc++ from GCC. Add-stdlib=libc++
and you will get an assertion failure. – Holt