Paolo Celati
Paolo Celati

Reputation: 312

No default arguments in constructor when using move constructor

When I added a move constructor to my child class, I found I can't use default arguments for the constructor. I've also tried using an overloaded constructor in base_class with zero arguments which calls the normal constructor with the arguments passed in manually but I'm having the same issue.

The error message from Visual Studio is: error C2512: 'child_class<base_class>': no appropriate default constructor available

class base_class {
public:
    base_class(int param1=1) {}
};

template <typename BaseType>
class child_class : public BaseType {
public:
    using BaseType::BaseType;
    child_class(child_class&& move_in)
        : BaseType(std::move(move_in)) {}
};

int main(int argc, char** argv) {
    child_class<base_class> instance1; // MSVC says no default constructor so doesn't compile.
    child_class<base_class> instance2(123); // No problem
}

Upvotes: 2

Views: 50

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 71989

This is probably an MSVC bug in the inherited constructor implementation. Clang compiles the code without complaint.

Upvotes: 3

Related Questions