std::nullopt_t(3)
β¨ NAME β¨
std::nullopt_t β bestie, it's literally the icon of "no thanks, I'm good" for optionals π
β¨ SYNOPSIS β¨
First things first, it's defined in the header <optional>:
struct nullopt_t; (since C++17)
So std::nullopt_t is this empty little class type, and its whole personality is flagging when an optional is straight-up uninitialized, like a cute little "no value here, bestie π
" vibe.
The main character moment? std::optional has a constructor that takes nullopt_t as its single argument, and that constructor creates an optional that does not contain a value. Periodt. β¨
Now for the rules, and yes we love a strict queen:
std::nullopt_t must be a non-aggregate LiteralType, and it cannot have a default constructor or an initializer-list constructor. Not negotiable.
It must also have a constexpr constructor that takes some implementation-defined literal type. Yes, that's a thing, and yes it's giving very specific. πΈ
β¨ NOTES β¨
Okay so why are the constructors so picky? The constraints on nullopt_t's constructors exist so that both op = {}; and op = nullopt; work as the syntax for disengaging an optional object. It's all for the vibes of a clean little reset. π§
A possible implementation of this class is:
struct nullopt_t {
constexpr explicit nullopt_t(int) {}
};
That's it. That's the whole baddie. Simple, effective, highkey iconic. π
β¨ SEE ALSO β¨
nulloptβ an object of typenullopt_t, and yes bestie it's a C++17 constant, so gaze upon it. π
That's the whole page, babe! Source is http://cppreference.com, version date 2024.06.10, and this adorable little manual page is filed under std::nullopt_t(3). Now go forth and be optional. ππ¦β¨
π¦ 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