BCS
BCS

Reputation: 78683

Inheriting-Constructors + In-Class-Initialization of non-default constructabe type fails

I am encountering the following error in my project:

error: use of deleted function ‘C::C(int)’ note: ‘C::C(int)’ is
implicitly deleted because the default definition would be ill-formed:
error: use of deleted function ‘M::M()’

This is the code I am using:

struct M {
    M(int){}
    M() = delete;  // Allowing this would work.
};

struct B {
    B(int) {}
    B() = delete;
};

struct C : public B {
    using B::B;
    M n = {5};

    // C(int i) : B(i) {}  // Adding this would work
};

C c{1};

Does anyone know why is this happening?


Clearly the language is willing to append more initialization on the end of the inherited constructor (as it's willing to call a default constructor). And clearly it's willing to implicitly add a call to the non-default constructor (the in class initialization) to the end of an explicitly defined constructor. But for some reason that I don't understand, it's not willing to do both at the same time.

According to this question, perfect forwarding isn't really perfect enough and shouldn't be used here.

Note: in the real case the constructor(s) for B are much more complex and subject to change, so manually forwarding stuff isn't really a viable option.

Upvotes: 15

Views: 230

Answers (1)

Related Questions