std::monostate(3)
π NAME π
Okay bestie, this is the std::monostate(3) page from the C++ Standard Library β the original banner spells it C++ Standard Libary, and Iβm keeping that typo energy alive π. The NAME is literally std::monostate - std::monostate, which is giving main-character repetition π
.
β¨ Synopsis β¨
Defined in header <variant>
struct monostate { }; (since C++17)
std::monostate is a unit type, fr fr β itβs meant to be that well-behaved empty alternative in std::variant. The iconic part: a variant of non-default-constructible types may list std::monostate as its first alternative, and that makes the variant itself default-constructible. No cap, thatβs the whole slay. π¦
π οΈ Member functions π οΈ
- constructor β
(implicitly declared) (public member function); itβs a trivial implicit default/copy/move constructor. Lowkey perfect. - destructor β
(implicitly declared) (public member function); itβs a trivial implicit destructor. - operator= β
(implicitly declared) (public member function); itβs trivial implicit copy/move assignment. Clean.
π₯ Non-member functions π₯
The crew is literally std::operator==, !=, <, <=, >, >=, <=>(std::monostate). Hereβs the exact signature-and-body situation, bestie:
constexpr bool operator==( monostate, monostate ) (1) (since C++17)
noexcept { return true; }
constexpr bool operator!=( monostate, monostate )
noexcept { return false; }
constexpr bool operator< ( monostate, monostate )
noexcept { return false; }
constexpr bool operator> ( monostate, monostate )
(since C++17)
noexcept { return false; }
(until C++20)
constexpr bool operator<=( monostate, monostate )
noexcept { return true; }
(2)
constexpr bool operator>=( monostate, monostate )
noexcept { return true; }
constexpr std::strong_ordering operator<=>(
monostate, monostate ) noexcept
{
(since C++20)
return std::strong_ordering::equal;
}
All instances of std::monostate compare equal β every single one, periodt. π
The page also says: the <, <=, >, >=, and != operators are synthesized from operator<=> (since C++20) and operator== respectively. Thatβs the tea. β
π§ Helper classes π§
std::hash<std::monostate> is here too:
template <> (since C++17)
struct std::hash<monostate>;
It specializes the std::hash algorithm for std::monostate. Obsessed with a specialized hash, honestly. π
π Example π
The page says // Run this code, so hereβs the whole example β code stays code, exactly:
#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';
}
ngl, thatstd::get<S>(var)line throws because we need to assign a value β the code comment literally says// throws! We need to assign a valueπ
Possible output:
std::get: wrong index for variant
std::get: 42
std::hash: 0xffffffffffffe19f
π See also π
- constructor β constructs the variant object (
public member function). Thatβs the see-also link, bestie. π«Ά
mwah π this page came from http://cppreference.com, dated 2024.06.10, and itβs manual page std::monostate(3) β byeee π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms