15

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.

Community
  • 1
  • 1
BCS
  • 75,627
  • 68
  • 187
  • 294
  • Is the problem in deleted `B()` ? The compiler can't write the default `C()` because it needs the default `B()` to exist... –  Aug 29 '14 at 19:00
  • Program compiles with clang++3.5 – dyp Aug 29 '14 at 19:03
  • 2
    As far as I understand the Standard, this program is actually well-formed. The implicitly-defined `C(int)` ctor should be defined as `C(int p) : B(static_cast(p)) {}` – dyp Aug 29 '14 at 19:04
  • It seems g++4.8 ignores the NSDMI in the "inherited" ctor (`C(int)`), which it should not. Can anyone confirm this on a recent g++? – dyp Aug 29 '14 at 19:08
  • Ah, sorry. I misunderstood. Looks like it should work, then. –  Aug 29 '14 at 19:14
  • 1
    Fails on [gcc5.0](http://melpon.org/wandbox/permlink/EAlTxnFS6ukd3rt3) trunk and [gcc4.9](http://coliru.stacked-crooked.com/a/20d44877e4a7a8c6) also. Can't find any existing bug reports on this either. You should report it - https://gcc.gnu.org/bugzilla/ – Praetorian Aug 29 '14 at 19:20

1 Answers1

6

This is a GCC Bug and has now been reported.

BCS
  • 75,627
  • 68
  • 187
  • 294