c++optional_t类型标记(n3527)
c++ optional_t type tag (n3527)
n3527建议将std::optional<T>
添加到c++中。作为其中的一部分,它定义了nullopt_t
类型标记。
gcc的(4.9)libstdc++定义optional_t
如下:
struct nullopt_t
{
enum class _Construct { _Token };
explicit constexpr nullopt_t(_Construct) { }
};
constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
clang的(3.6)libc++将其定义为:
struct nullopt_t
{
explicit constexpr nullopt_t(int) noexcept {}
};
constexpr nullopt_t nullopt{0};
我的问题是:为什么要这样做,这(看起来)过于复杂了?
换句话说,为什么不能定义如下:
struct nullopt_t { };
constexpr nullopt_t nullopt { };
例如,std::defer_lock_t
和其他内容就是这样在标准库中定义的。
在这里回答了我自己,但这要归功于kakkoko让我走上了正轨。
相关问题链接到一个新的修订版n3793,该修订版在op={}语法段落中阐述了nullopt_t
的额外复杂性。
简而言之,为了避免op = {}
语法的歧义,nullopt_t
是以这种方式声明的,使其成为非DefaultConstructible
。你可以阅读上面的段落了解更多的细节。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- c++optional_t类型标记(n3527)