c++ - 空结构是由 C++ 标准定义的吗?

标签 c++ c++11

是否有任何 std::empty 结构或类似的结构,或者我需要自己定义:

struct empty{};

这可以很好地与 std::conditional 或其他新的 std 功能结合使用,我想知道标准是否定义了它。

最佳答案

有一个部分可以将这种结构添加为 Variant proposal (n4542) 的一部分。 .

经过投票,

What do we want to call the “empty_t” stand-in type?
empty_t 4
empty 4
one_t 1
blank 6
blank_t 7
monostate 7

Runoff:
blank* 3
monostate 8

商定的名称是:std::monostate。


定义如下:

// 2.?, Explicitly default-constructed alternative
struct monostate {};
bool operator<(const monostate&, const monostate&) constexpr
{ return false; }
bool operator>(const monostate&, const monostate&) constexpr
{ return false; }
bool operator<=(const monostate&, const monostate&) constexpr
{ return true; }
bool operator>=(const monostate&, const monostate&) constexpr
{ return true; }
bool operator==(const monostate&, const monostate&) constexpr
{ return true; }
bool operator!=(const monostate&, const monostate&) constexpr
{ return false; }

关于c++ - 空结构是由 C++ 标准定义的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16666871/

相关文章:

c++ - 从该对象的方法或另一个类构造函数中删除该对象是否被认为是不好的做法?

c++ - 通过引用使用 const int 创建静态数组

c++ - std::unordered_set 的元素的迭代顺序是否保证始终相同?

c++ - std::istringstream::imbue 是否拥有传递的对象

c++ - 内存屏障作用域

c++ - 对 operator+ 和/或 operator+= 使用 move 语义有意义吗?

c++ - C++ reference_wrapper vector 使用错误的值填充

c++ - C++ 中使用 <random> 的随机数顺序

c++ - 使用 valgrind 启动时,不稳定的 cython 扩展不会崩溃

python - 使用 python setup.py 编译和安装 C++ 程序