std::monostate
性病::单一国家
Defined in header | | |
---|---|---|
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 | | |
---|
专攻std::hash
算法std::monostate
...
例
二次
#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';
}
二次
产出:
二次
12
二次
另见
(constructor) | constructs the variant object (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。