std::monostate - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::monostate

From cppreference.com
< cpp‎ | utility‎ | variant

Defined in header `<variant>` |  |
struct monostate { } |  | (since C++17)

Unit type intended for use as a well-behaved empty alternative in std::variant. In particular, a variant of non-default-constructible types may list `std::monostate` as its first alternative: this makes the variant itself default-contructible.

### [edit] Member functions

(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)

### [edit] Non-member functions

##

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; } |  |

All instances of `std::monostate` compare equal.

### Exceptions

`noexcept` specification:
noexcept

### [edit] Helper classes

## std::hash<std::monostate>

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

Specializes the std::hash algorithm for `std::monostate`

### [edit] Example

Run this code

    #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';
    }

Output:

    12

### [edit] See also

(constructor) | constructs the variant object (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/variant/monostate&oldid=91902"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template