跳到主要内容

std::monostate

定义于头文件 <variant>

C++17

struct monostate { };

单元类型,旨在用作 std::variant 中行为良好的空替代。特别是,非默认可构造类型的变体可以将 std::monostate 列为其第一个替代:这使得变体本身可以默认构造。

成员函数

pub(构造函数)(隐式声明)平凡隐式默认/复制/移动构造函数
(公共成员函数)
pub(析构函数)(隐式声明)平凡隐式析构函数
(公共成员函数)
pub(operator=)(隐式声明)平凡隐式复制/移动赋值
(公共成员函数)

非成员函数


std::operator==, !=, <, <=, >, >=, <=>(std::monostate)

constexpr bool operator==( monostate, monostate ) noexcept { return true; }     // 1) (since C++17)
constexpr bool operator!=( monostate, monostate ) noexcept { return false; }
constexpr bool operator< ( monostate, monostate ) noexcept { return false; }
constexpr bool operator> ( monostate, monostate ) noexcept { return false; }
constexpr bool operator<=( monostate, monostate ) noexcept { return true; }
constexpr bool operator>=( monostate, monostate ) noexcept { return true; } // 2) (C++17)
constexpr std::strong_ordering operator<=>( monostate, monostate ) noexcept     
{
return std::strong_ordering::equal;
} // 2) (since C++20)

所有 std::monostate 的实例都比较相等。

<, <=, >, >= 和 != 运算符分别由 operator<=> 和 operator== 合成。(自 C++20 起)


辅助类


std::hash<std::monostate>

template <>
struct std::hash<monostate>; // 2) (since C++17)

std::monostate 特化 std::hash 算法。


示例

#include <cassert>
#include <iostream>
#include <variant>

struct S
{
S(int i) : i(i) {}
int i;
};

int main()
{
// Without the monostate type this declaration will fail.
// This is because S is not default-constructible.
std::variant<std::monostate, S> var;
assert(var.index() == 0);

try
{
std::get<S>(var);
// throws! We need to assign a value
}
catch(const std::bad_variant_access& e)
{
std::cout << e.what() << '\n';
}

var = 42;
std::cout
<< "std::get: "
<< std::get<S>(var).i << '\n'
<< "std::hash: "
<< std::hex << std::showbase
<< std::hash<std::monostate>{}(std::monostate{})
<< '\n';
}
可能结果
std::get: wrong index for variant
std::get: 42
std::hash: 0xffffffffffffe19f

std::monostate

定义于头文件 <variant>

C++17

struct monostate { };

单元类型,旨在用作 std::variant 中行为良好的空替代。特别是,非默认可构造类型的变体可以将 std::monostate 列为其第一个替代:这使得变体本身可以默认构造。

成员函数

pub(构造函数)(隐式声明)平凡隐式默认/复制/移动构造函数
(公共成员函数)
pub(析构函数)(隐式声明)平凡隐式析构函数
(公共成员函数)
pub(operator=)(隐式声明)平凡隐式复制/移动赋值
(公共成员函数)

非成员函数


std::operator==, !=, <, <=, >, >=, <=>(std::monostate)

constexpr bool operator==( monostate, monostate ) noexcept { return true; }     // 1) (since C++17)
constexpr bool operator!=( monostate, monostate ) noexcept { return false; }
constexpr bool operator< ( monostate, monostate ) noexcept { return false; }
constexpr bool operator> ( monostate, monostate ) noexcept { return false; }
constexpr bool operator<=( monostate, monostate ) noexcept { return true; }
constexpr bool operator>=( monostate, monostate ) noexcept { return true; } // 2) (C++17)
constexpr std::strong_ordering operator<=>( monostate, monostate ) noexcept     
{
return std::strong_ordering::equal;
} // 2) (since C++20)

所有 std::monostate 的实例都比较相等。

<, <=, >, >= 和 != 运算符分别由 operator<=> 和 operator== 合成。(自 C++20 起)


辅助类


std::hash<std::monostate>

template <>
struct std::hash<monostate>; // 2) (since C++17)

std::monostate 特化 std::hash 算法。


示例

#include <cassert>
#include <iostream>
#include <variant>

struct S
{
S(int i) : i(i) {}
int i;
};

int main()
{
// Without the monostate type this declaration will fail.
// This is because S is not default-constructible.
std::variant<std::monostate, S> var;
assert(var.index() == 0);

try
{
std::get<S>(var);
// throws! We need to assign a value
}
catch(const std::bad_variant_access& e)
{
std::cout << e.what() << '\n';
}

var = 42;
std::cout
<< "std::get: "
<< std::get<S>(var).i << '\n'
<< "std::hash: "
<< std::hex << std::showbase
<< std::hash<std::monostate>{}(std::monostate{})
<< '\n';
}
可能结果
std::get: wrong index for variant
std::get: 42
std::hash: 0xffffffffffffe19f