std::monostate
From cppreference.com
 
 
 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Utilities library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
 Type support (basic types, RTTI)
 Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
 Coroutine support (C++20)
 Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
  (C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
 General utilities
 Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
  (C++20)(C++20)(C++20)
(C++20)
 Swap and type operations 
 Common vocabulary types
std::variant 
 
 
(C++26)
monostate
Defined in header 
 
 
<variant> 
 Defined in header 
 
 <utility> 
 (since C++26)
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.
Contents
[edit] Member functions
(constructor)
(implicitly declared)
(public member function)
(destructor)
(implicitly declared)
(public member function)
operator=
(implicitly declared)
(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; }
 
 (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 true; }
(until C++20)
constexpr std::strong_ordering operator<=>( monostate, monostate ) noexcept
 
 (since C++20) 
{
    return std::strong_ordering::equal;
All instances of std::monostate compare equal.
The <, <=, >, >=, and != operators are synthesized from operator<=> and operator== respectively.
[edit] Helper classes
std::hash<std::monostate>
template <>
struct std::hash <monostate>;
 
 (since C++17) 
struct std::hash <monostate>;
Specializes the std::hash  algorithm for std::monostate.
[edit] Example
Run this code
#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