2015-09-11 8 views
9

Non capisco perché non sia possibile compilare una classe che ha sia un membro (non di default costruibile) con un inizializzatore brace-or-equal e un costruttore ereditato. g ++ dice:Ereditarietà dei costruttori e inizializzatori bret-or-equal

test.cpp:22:15: error: use of deleted function ‘Derived::Derived(float)’
Derived d(1.2f);

test.cpp:16:13: note: ‘Derived::Derived(float)’ is implicitly deleted
because the default definition would be ill-formed:
using Base::Base;

test.cpp:16:13: error: no matching function for call to ‘NoDefCTor::NoDefCTor()’
test.cpp:5:1: note: candidate:
NoDefCTor::NoDefCTor(int) NoDefCTor(int) {}

codice che non riesce a compilare (in g ++ 5.1):

struct NoDefCTor 
{ 
    NoDefCTor(int) {} 
}; 

struct Base 
{ 
    Base(float) {} 
}; 

struct Derived : Base 
{ 
    using Base::Base; 
    NoDefCTor n2{ 4 }; 
}; 

int main() 
{ 
    Derived d(1.2f); 
} 

codice che compila, ma mai utilizza costruttore NoDefCTor s' di default (nonostante quanto pare averne bisogno!):

struct NoDefCTor 
{ 
    NoDefCTor(int) {} 
    NoDefCTor() = default; 
}; 

struct Base 
{ 
    Base(float) {} 
}; 

struct Derived : Base 
{ 
    using Base::Base; 
    NoDefCTor n2{ 4 }; 
}; 

int main() 
{ 
    Derived d(1.2f); 
} 

Non mi piace l'idea di avere un costruttore predefinito quando non ne ho bisogno. Nota a margine entrambe le versioni compilano (e si comportano) bene su MSVC14.

+2

Questo bug gcc? https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62310 – dyp

+3

Oppure questo bug di gcc? https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67054 – dyp

+0

@dyp grazie. È il secondo bug in effetti. Pensavo fosse una cosa del linguaggio. – maxbc

risposta

6

Questo è un gcc bug, #67054. Confrontando la segnalazione di bug per alltaken380 al caso del PO:

// gcc bug report      // OP 
struct NonDefault      struct NoDefCTor 
{          { 
    NonDefault(int) {}      NoDefCTor(int) {} 
};          }; 

struct Base        struct Base 
{          { 
    Base(int) {}        Base(float) {} 
};          }; 

struct Derived : public Base    struct Derived : Base 
{          { 
    NonDefault foo = 4;      NoDefCTor n2{ 4 }; 

    using Base::Base;      using Base::Base; 
};          }; 

auto test()        int main() 
{          { 
    auto d = Derived{ 5 };     Derived d(1.2f); 
}          } 

Possiamo anche provare questo sulle recenti versioni 6.0 gcc, e non riesce ancora a compilare. clang ++ 3.6 e, secondo l'OP, MSVC14 accetta questo programma.

Problemi correlati