Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the constructor of std::in_place_t defaulted and explicit?

cppreference shows the following definition of std::in_place_t:

struct in_place_t {
    explicit in_place_t() = default;
};
inline constexpr std::in_place_t in_place{};

Why have they added an explicit and defaulted constructor? Why it isn't left out? What are the benefits?

like image 557
Martin Fehrs Avatar asked Mar 18 '19 15:03

Martin Fehrs


2 Answers

You want a type like this to only be explicitly constructible, because it exists to denote a particular kind of constructor overload, in places where {} might reasonably be found.

Consider the following constructions

std::optional<DefaultConstructible> dc1({}); // dc1 == std::nullopt
std::optional<DefaultConstructible> dc2(std::in_place); // dc2 == DefaultConstructible()
like image 122
Caleth Avatar answered Nov 14 '22 16:11

Caleth


If you leave out the constructor it will not be explicit. If you don't = default it it will not be trivial.

So, if you want the constructor to be explicit and you also want it to remain trivial, what you see is the only option available.

like image 30
Jesper Juhl Avatar answered Nov 14 '22 17:11

Jesper Juhl