deworde
deworde

Reputation: 2789

Why does std::unique_lock have a default constructor?

I was watching this talk by Louis Brandy, when a fellow viewer asked this obvious question:

Why does std::unique_lock have a default constructor?

And now I have to know.

Upvotes: 7

Views: 1066

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 71989

unique_lock is movable. It has a moved-from state that is basically "empty", not associated with any mutex. This state can also be reached by calling release().

Given that this state exists, and the benefits of having a default-constructor (such as being able to create arbitrarily-sized dynamic arrays), it's a good idea to add the default constructor that creates the same state.

Upvotes: 7

Related Questions