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?
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() .
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With