Why does an abstract class need to call its virtual parent constructors?

http://ideone.com/tmdzkn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

struct A
{
	A() = delete;
	A(int x)
	: x(x)
	{
	}
	virtual ~A() = 0;
	int x;
};
A::~A() = default;

struct B : virtual A
{
	B()
	//: A(-1)
	{
	}
	virtual ~B() = 0;
};
B::~B() = default;

struct C : B
{
	C()
	: A(7)
	, B()
	{
	}
};

int main()
{
	C c;
	std::cout << c.x << std::endl;
}
prog.cpp: In constructor ‘B::B()’:
prog.cpp:19:2: error: use of deleted function ‘A::A()’
  {
  ^
prog.cpp:5:2: error: declared here
  A() = delete;
  ^
The program fails to compile because the constructor for B is malformed for not calling A::A(int). But why does it need to if B is abstract? Whatever constructor from A I call in B will always get ignored, right? Is there any reason not to allow the program to compile?

EDIT: Modified working program: http://ideone.com/N7py90
Note the call A(-1) will never be used.
Last edited on
> Whatever constructor from A I call in B will always get ignored, right?

Right.
[Note: An abstract class is never a most derived class, thus its constructors never initialize virtual base classes, therefore the corresponding mem-initializers may be omitted. —end note] - IS


> Is there any reason not to allow the program to compile?

The compiler may not be conforming wrt this.

With LLVM/clang++ this compiles cleanly.
With GNU/g++ or Mirosoft C++, it does not.

http://coliru.stacked-crooked.com/a/93842129f7f4e0a5
http://rextester.com/HSB19355
OK, so it's a compiler bug - thanks!
Topic archived. No new replies allowed.