vendredi 28 octobre 2016

code does not compile in g++ while it does in clang++

So I have this really short code:

test.cpp

class Base {
    public:
        Base(int i) {};
};

class Child : public virtual Base {
    using Base::Base;
};

int main(int argc, char * argv[]) {
    auto *child = new Child(1);
    return 0;
};

It compiles well under clang++ (3.8.0):

$ clang++ test.cpp -std=c++11

while it fails under g++ (5.4.0):

$ g++ test.cpp -std=c++11
test.cpp: In function ‘int main(int, char**)’:
test.cpp:14:30: error: use of deleted function ‘Child::Child(int)’
     auto *child = new Child(1);
                              ^
test.cpp:8:17: note: ‘Child::Child(int)’ is implicitly deleted because the default definition would be ill-formed:
     using Base::Base;
                 ^
test.cpp:8:17: error: no matching function for call to ‘Base::Base()’
test.cpp:3:9: note: candidate: Base::Base(int)
         Base(int i) {};
         ^
test.cpp:3:9: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: constexpr Base::Base(const Base&)
 class Base {
       ^
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided
test.cpp:1:7: note: candidate: constexpr Base::Base(Base&&)
test.cpp:1:7: note:   candidate expects 1 argument, 0 provided

For some reason g++ expects Base class to have the default constructor. Why is that?

Aucun commentaire:

Enregistrer un commentaire