Bug 71804 - Default-generated constructor does not construct base class
Summary: Default-generated constructor does not construct base class
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-07-07 21:37 UTC by Guille
Modified: 2016-07-08 03:05 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Guille 2016-07-07 21:37:11 UTC
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;
}
Comment 1 Marc Glisse 2016-07-07 21:44:36 UTC
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.
Comment 2 Guille 2016-07-07 23:03:38 UTC
(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.
Comment 3 Guille 2016-07-08 03:05:12 UTC
Sorry, this was silly. I'll try to get some sleep before posting something at 5am.