这里描述了为 c++ 提出的对象的nullopt_tand :nulloptoptional
struct nullopt_t{see below}; constexpr nullopt_t nullopt(unspecified);[...] 类型 nullopt_t 不应有默认构造函数。它应该是文字类型。常量 nullopt 应使用文字类型的参数进行初始化。
其原因在文档的 op = {} 语法章节中进行了解释:为了op = {}明确起见,必须采用一些技巧,其中之一就是nullopt_t不能默认构造。
我的问题是文字类型在这里意味着什么?我发现了这个SO 帖子。所以在我看来,只要另一个空班就可以了。它也可以是一个构造函数int吗?
一个最小的符合nullopt_t类是什么样子的?
像这样的东西:
struct nullopt_t_construct_tag_t{};
struct nullopt_t {
nullopt_t() = delete; // I know declaring it as deleted is redundant
constexpr nullopt_t(nullopt_t_construct_tag_t) {};
};
constexpr nullopt_t nullopt(nullopt_t_construct_tag_t{});
或这个:
struct nullopt_t {
nullopt_t() = delete;
constexpr nullopt_t(int) {};
};
constexpr nullopt_t nullopt(0);