Vincent
Vincent

Reputation: 60371

What do explicitly-defaulted constructors do?

Consider the following:

template <class T>
struct myclass
{
    using value_type = T;
    constexpr myclass() = default;
    constexpr myclass(const myclass& other) = default;
    constexpr myclass(const myclass&& other) = default;
    T value;
};

Upvotes: 7

Views: 7317

Answers (1)

M.M
M.M

Reputation: 141554

They aren't equivalent to any function bodies. There are small but significant differences between the three cases: = default, allowing implicit generation, and the nearest equivalent function body.

The following links explain in more detail:

I couldn't find a good link about copy-constructor; however similar considerations as mentioned in the other two links will apply.


myclass<int> x; does not set value to 0.

The defaulted move-constructor (if you made it a non-const reference) moves each movable member (although I think there is a special case where if there is a non-movable base class, weird things happen...)

Upvotes: 7

Related Questions