TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What's the point of std:monostate? You can't do anything with it

115 pointsby luu10 months ago

17 comments

nostrademons10 months ago
A &quot;monostate&quot; in the design-patterns lingo of 20 years ago was a class with only static member variables, basically where all state is shared state. It was supposed to be alternative to the Singleton pattern where you don&#x27;t need all those .getInstance() calls and instead can just default-construct an instance, any instance, to get access to the shared state. It fell out of favor because usage was fairly error-prone and surprising to programmers who are not familiar with the pattern. Most people expect that when you create a new instance, you are actually creating new state, but monostate intentionally makes each new instance just a window on the shared global state.<p>I would&#x27;ve thought that the C++ template class would be just a marker interface to use on a monostate, so that users of the class <i>know</i> that it has shared state. But it seems like usage patterns in the article are very different from that, and all the comments here are ignorant of the history of the monostate pattern and befuddled at its intended usage. Maybe it was added to the standard by someone familiar with the design pattern, but they didn&#x27;t do a good job with education and documentation to explain to everyone else what it was for?
评论 #41039053 未加载
评论 #41036840 未加载
pavlov10 months ago
There’s a secret competition between C++ and JavaScript to come up with more variants of nothing.
评论 #41035993 未加载
评论 #41043950 未加载
评论 #41039088 未加载
评论 #41033631 未加载
tpolzer10 months ago
What&#x27;s really weird to me is not that C++ has a unit type and picked a weird name for it (that&#x27;s just C++). The weird thing is how many unit types it has:<p>- std::nullopt_t<p>- std::nullptr_t<p>- std::monostate<p>- std::tuple&lt;&gt;<p>And I&#x27;m sure there&#x27;s more.
评论 #41036762 未加载
评论 #41036382 未加载
lloydatkinson10 months ago
So it appears this follows the terrible C++ &quot;naming things with the wrong name on purpose&quot; trend, with the biggest example being naming a variable length array (a list, collection, etc) `std:vector` even though Vector was already a well known word with a very different meaning.<p>The word&#x2F;type they wanted for this was the Unit type: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Unit_type" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Unit_type</a> and in that list it even states the `std:monotype` is a Unit.
评论 #41033124 未加载
评论 #41033146 未加载
评论 #41033010 未加载
评论 #41033354 未加载
评论 #41036192 未加载
评论 #41038139 未加载
评论 #41033824 未加载
amne10 months ago
so &quot;std::nothing&quot; was taken? or &quot;std::none&quot; ? or anything else that would make it obvious this type is a fancy way to say void?
评论 #41033737 未加载
评论 #41033349 未加载
评论 #41033027 未加载
评论 #41037162 未加载
评论 #41040676 未加载
nmeofthestate10 months ago
Another use of std::monostate is as a special &quot;unset&#x2F;don&#x27;t care&quot; value for a template parameter. eg<p><pre><code> template&lt;typename T = std::monostate&gt; class C { ... if constexpr (!std::is_same_v&lt;T, std::monostate&gt; { &#x2F;&#x2F; T-related behaviour here } };</code></pre>
评论 #41040094 未加载
评论 #41035757 未加载
JackYoustra10 months ago
There&#x27;s a fun thing like this in Swift too! `Void` is an empty tuple, and has all of the related constraints (can&#x27;t conform to protocols, being the most salient one). If you have a type that has to conform to, say, `Equatable` or `Codable` you should instead use `Never?` which you can conform to most protocols via throwing `fatalError` on an extension of `Never` to the protocol.<p>Anyway, I write basically this on my blog for a more thorough explanation: <a href="https:&#x2F;&#x2F;www.jackyoustra.com&#x2F;blog&#x2F;non-equatable-void" rel="nofollow">https:&#x2F;&#x2F;www.jackyoustra.com&#x2F;blog&#x2F;non-equatable-void</a>
评论 #41042701 未加载
ape410 months ago
Good thing they made this instead of expanding the standard library to be more like Java&#x27;s or Python&#x27;s. It still only contains the most basic functions, and std::monostate ;)
furyofantares10 months ago
Gerard: But it doesn&#x27;t <i>do</i> anything!<p>Hans: No -- it <i>does</i> nothing.<p><a href="https:&#x2F;&#x2F;scryfall.com&#x2F;card&#x2F;wth&#x2F;154&#x2F;null-rod" rel="nofollow">https:&#x2F;&#x2F;scryfall.com&#x2F;card&#x2F;wth&#x2F;154&#x2F;null-rod</a>
评论 #41038078 未加载
kazinator10 months ago
It&#x27;s poor name. If it has no members, it holds no bits. Therefore it has no state. It&#x27;s not enough for an object to exist in order to hold state. It must be capable of distinguishing at least between two values, like true or false. If something doesn&#x27;t hold state, the word <i>state</i> has no business in its name.<p>This thing is just a counterpart to <i>void</i> that is a class. A better name would be <i>voidclass</i> or something along those lines.<p>(There is a Monostate pattern, but that involves a class with state: just all the state is static. It&#x27;s basically like a module with global variables. Completely different thing.)
评论 #41037383 未加载
leecommamichael10 months ago
Raymond is a very smart and productive person, and is not maligning C++ at all in this article. It makes me want to reassess my bittersweet perspective on the language.
评论 #41036678 未加载
shadowgovt10 months ago
So... Isn&#x27;t the fact that you can&#x27;t default-construct that variant without `monostate` working-as-intended?<p>Not everything can or should be default-constructed or default-constructible. That does complicate initialization sometimes (i.e. there are practical reasons to &quot;suspend&quot; construction until you have all the needed data), but you&#x27;re not avoiding breaking type safety by adding a `monostate`, you&#x27;re just giving yourself the &quot;could be null&quot; headache.
formerly_proven10 months ago
Seems almost intentionally confusingly written. Why not use void if monostate is like void? Ah, because monostate is actually entirely unlike void. void has zero values, mono state exactly one.
评论 #41033171 未加载
评论 #41033215 未加载
评论 #41033059 未加载
vasilipupkin10 months ago
this is crazy confusing stuff that should be banned by most sensible internal coding standards.
revskill10 months ago
Is it like a singleton without any method ?
评论 #41034641 未加载
ramon15610 months ago
So the upside is that you can put it at the beginning of a function? Wild, bro
评论 #41033219 未加载
评论 #41033173 未加载
loa_in_10 months ago
It&#x27;s probably more of use to compiler&#x2F;runtime developers than end users if the language.
评论 #41033057 未加载
评论 #41036351 未加载