[英]Why is the constructor of std::in_place_t defaulted and explicit?
cppreference顯示了std::in_place_t
的以下定義:
struct in_place_t {
explicit in_place_t() = default;
};
inline constexpr std::in_place_t in_place{};
為什么他們添加了一個explicit
默認構造函數? 為什么不被遺漏? 有什么好處?
如果省略構造函數,它將不explicit
。 如果你不= default
它,它將不會是微不足道的 。
因此,如果您希望構造函數是explicit
並且您還希望它保持微不足道,那么您看到的是唯一可用的選項。
你希望這樣的類型只能explicit
構造,因為它可以在合理找到{}
地方表示特定類型的構造函數重載。
考慮以下結構
std::optional<DefaultConstructible> dc1({}); // dc1 == std::nullopt
std::optional<DefaultConstructible> dc2(std::in_place); // dc2 == DefaultConstructible()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.