Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicitly deleted default constructor works in C++14

I was doing some tests for some code that did not compile, and I discovered that this code:

struct A {
    A(int) {};
    virtual void foo() = 0;
};

struct B : public virtual A {
    virtual void bar() = 0;
};

struct C : public B {
    C() : A(1) {}
    virtual void foo() override {}
    virtual void bar() override {}
};

int main() {
    C c;
    return 0;
}

In C++11 fails to compile (on g++ 7.0.1) with ‘B::B()’ is implicitly deleted because the default definition would be ill-formed, while in C++14 compiles successfully.

I've tried to find out which new feature of C++14 allowed this to work, to no avail. The description in cppreference does not mention anything of the sort it seems.

Why can this code compile in C++14 but not in C++11?

like image 860
Svalorzen Avatar asked Apr 14 '17 11:04

Svalorzen


People also ask

What is implicit default constructor in C++?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Should I delete default constructor C++?

Deleting the default constructor of a class is a good idea when there are multiple choices for the default or uninitialised state. For example, suppose I have a class, template<typename F> class Polynomial; which represents a polynomial over a field, F .

Can default constructor be deleted?

The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true: T has a member of reference type without a brace-or-equal initializer.

Can a default constructor be called implicitly?

No default constructor is created for a class that has any constant or reference type members. A constructor of a class A is trivial if all the following are true: It is implicitly defined or explicitly defaulted.


1 Answers

Definitively it's a bug in gcc 7 because when I have checked out your code in a online compiler with gcc 7+, it has worked perfectly without any kind of problem.

So here I give you that IDE online where you can set your favorite compiler and try to do tests, if you want.

https://godbolt.org/

Sorry for I can't help you better but I couldn't reproduce your error.

like image 199
Isguma Avatar answered Sep 28 '22 02:09

Isguma