Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A "monostate" 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'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.

I would've thought that the C++ template class would be just a marker interface to use on a monostate, so that users of the class know 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't do a good job with education and documentation to explain to everyone else what it was for?



That just sounds like a different thing with the same name. It's nowhere close to the purpose of std::monostate.


That’s the GP’s point. It is strange that std::monostate was chosen as the name, given that the different Monostate pattern [0] was fairly well established in C++ circles.

[0] https://wiki.c2.com/?MonostatePattern

(originally discussed in http://ftp.math.utah.edu/pub/tex/bib/cppreport.html#White:19...)


I considered that, but a lot of their comment seems to be about changing the class's behaviour to reflect the functionality they were expecting, rather than just giving it a different name. Especially this bit:

> I would've thought that the C++ template class would be just a marker interface to use on a monostate, so that users of the class know that it has shared state.

Also, if that's what they really meant then they surely could've have written a far sorter comment that simply says this name is already taken and it should be called something else. They don't seem to be saying that at all.

(Not that it affects my original point, but FWIW that linked meaning of monostate isn't common in my experience, and it sounds like a truly awful idea: if your state is really good then be honest about it and use free functions. So it hardly seems worth reserving a useful word for it over the concept std::monostate is actually about.)


BTW I saw your deleted reply about the point of monostate, thanks. For something that has to implement an existing interface, I can see the possible benefit.


Yeah I actually haven’t used the pattern much myself, so I started to doubt the relevance. Once you start having multiple separate monostates implementing the same interface, and also limit their creation/instantiation in the sense that the code that uses the monostate objects is separate from the code that creates them, the difference to regular objects/classes becomes a bit murky.


Yeah. The design pattern sounds like there is one state, and it is shared. What STL has looks like, all instances look the same, hence only one state is possible. They are homonyms with slightly different etymologies.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: