The following code (below) fails. The output of the program is: B() -> INSERT: 0x7fff570c8b2f ~B() -> ERASE: 0x7fff570c8b2f ~B() -> ERASE: 0x7fff570c8b40 Assertion failed: (!all.empty()), function ~B, file gcc_bug.c, line 20. Two default-generated constructors are generated for A, one has no arguments, and another is a copy/move ctor. As you can see, the former default-generated constructor constructs the base class B, but the latter doesn't. This is a refinement of a previous report, which was not presented correctly. ***************************************** #include <set> #include <iostream> #include <cstdint> #include <cassert> #include <experimental/optional> std::set<std::uintptr_t> all; struct B { B() { std::cerr << "B() -> INSERT: " << this << "\n"; all.insert((std::uintptr_t)this); } ~B() { std::cerr << "~B() -> ERASE: " << this << "\n"; assert(!all.empty()); // FAILS assert(all.find((std::uintptr_t)this) != all.end()); // FAILS all.erase((std::uintptr_t)this); } }; struct A : B {}; static std::experimental::optional<A> f() { A a; return a; } int main() { auto a = f(); return 0; }
This is not the right place to learn about C++. Before filing a bug report about a compiler, please find another compiler that behaves differently. If all compilers behave the same, it is unlikely to be a bug.
(In reply to Marc Glisse from comment #1) > This is not the right place to learn about C++. Before filing a bug report > about a compiler, please find another compiler that behaves differently. If > all compilers behave the same, it is unlikely to be a bug. Right you are. Clang does the same. It's not exactly the first time I use c++, still amazed at this. I'll look where the standard specifies this, I couldn't find reference to it online. I find it hard not assume something isn't quite right when one can create simple code with non-matching ctor/dtor. Thanks for the clarification, I'll try to find out about this elsewhere.
Sorry, this was silly. I'll try to get some sleep before posting something at 5am.