A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../algorithm/../utility/variant/monostate.html below:

std::monostate - cppreference.com

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-constructible.

[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 std::operator==, !=, <, <=, >, >=, <=>(std::monostate)

constexpr bool operator==( monostate, monostate ) noexcept { return true; }

(1) (since C++17) (2) 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; }
(since C++17)
(until C++20) constexpr std::strong_ordering operator<=>( monostate, monostate ) noexcept

{
    return std::strong_ordering::equal;

}
(since C++20)

All instances of std::monostate compare equal.

The <, <=, >, >=, and != operators are synthesized from operator<=> and operator== respectively.

(since C++20) [edit] Helper classes std::hash<std::monostate> (since C++17)

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

[edit] Example
#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';
}

Possible output:

std::get: wrong index for variant
std::get: 42
std::hash: 0xffffffffffffe19f
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4