首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::monostate

Defined in header <variant>

struct monostate { }

(since C++17)

单元类型,以便作为行为良好的空选项使用。std::variant特别是,非默认可构造类型的变体可能会列出。std::monostate作为它的第一个选择:这使得变体本身是可重构的。

成员函数

(constructor) (implicitly declared)

trivial implicit default/copy/move constructor (public member function)

(destructor) (implicitly declared)

trivial implicit destructor (public member function)

operator= (implicitly declared)

trivial implicit copy/move assignment (public member function)

非会员职能

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; } constexpr bool operator==(monostate, monostate) noexcept { return true; } constexpr bool operator!=(monostate, monostate) noexcept { return false; }

帮助者类

散列<std::monstate>

template <> struct std::hash<monostate>;

专攻std::hash算法std::monostate...

二次

代码语言:javascript
复制
#include <variant>
#include <iostream>
 
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; 
 
    // var.index() is now 0 - the first element
    // std::get<S> will throw! We need to assign a value
 
    var = 12; 
 
    std::cout << std::get<S>(var).i << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
12

二次

另见

(constructor)

constructs the variant object (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券