Virtual inheritance vs. non-default constructors

Viewed 1435

This code is rejected by (at least) MSVC, ICC, and GCC:

class A {
public:
    A( int ) {  }
};

class B: virtual public A {
public:
    //B(): A( -1 ) {  } // uncomment to make it compilable
    virtual void do_something() = 0;
};

class C: public B {
public:
    C(): A( 1 ) {  }
    virtual void do_something() {  }
};

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

on the basis of

error : no default constructor exists for class "A"
    class B: virtual public A {
                            ^
            detected during implicit generation of "B::B()" at line 14

Questions:

  1. If the code is indeed invalid, how exactly does this follow from the standard? AFAICT, 10.4/2 and 1.8/4 taken together imply that B cannot be a type of the most derived class, and therefore from 12.6.2/10 we have that B can never, ever call A's constructors. (The section numbers are for C++11.)

  2. If the code is valid, are compilers violating the standard by requiring the presence of constructors they could not possibly call? Note that not only they want to call A::A() from B::B(), but they want to do it while compiling C::C() (double weird).

P.S. This was originally asked on the ICC forum, but posted here due to not being limited to this compiler (and no details forthcoming).

3 Answers
Related