C++17 introduced std::monostate
, a dummy type with no members and trivial functions, primarily used when no action is needed. Despite its simplicity, std::monostate
plays a crucial role in scenarios like coroutines and std::variant
, where a default-constructible placeholder type is required.
What’s the point of std::monostate? You can’t do anything with it!
by Raymond Chen
From the article:
C++17 introduced
std::
, and I used it as a placeholder to represent the results of a coroutine that produces nothing. In the comments, Neil Rashbrook asked what you are expected to do with amonostate std::
, seeing as has no members and only trivial member functions.monostate The answer is “nothing”.
The purpose of
std::
is to be a dummy type that does nothing. All instances are considered equal to each other. It is basically this:monostate struct monostate {}; // plus relational operators and a hash specializationYou can see it in libcxx (LLVM/clang), libstdc++ (gcc), and stl (msvc).
Add a Comment
Comments are closed.