What’s the point of std::monostate? You can’t do anything with it! -- Raymond Chen

RaymondChen_5in-150x150.jpgC++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::monostate, 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 a std::monostate, seeing as has no members and only trivial member functions.

The answer is “nothing”.

The purpose of std::monostate is to be a dummy type that does nothing. All instances are considered equal to each other. It is basically this:

struct monostate {};
// plus relational operators and a hash specialization

You can see it in libcxx (LLVM/clang)libstdc++ (gcc), and stl (msvc).

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.