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

Single-function classes make sense if you want the computation to happen lazily on-demand (it may be resource-intensive in CPU, GPU, storage, I/O, etc). The class acts as the place to hold the parameters needed for it and the result if/when it is computed. There's nothing wrong with that.

Down the line you may often need the capability to discard the computation to save storage (and maybe re-do it at a later time), at which point it is not even a single-function class anymore.



> The class acts as the place to hold the parameters needed for it and the result if/when it is computed. There's nothing wrong with that.

Right, and the constness problem can be overcome by making some fields mutable. This is exactly what "mutable" is for.

If the requirement is to have a lazy, memoized computation, then a class is good. If the requirement is to have an eager, non-memoized computation, a function is good. The article keeps shifting the goalposts and beating strawmen that implement different specifications. It's not very good at making the point it thinks it's making.


Mutable in C++11 land holds a weird space because the threading model says that const member methods are thread safe, which mutable member variables are not.


> threading model says that const member methods are thread safe

that's only true for standard library objects, although it is an useful guideline for all code.


I don't think that's true.

> A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this

Consider the case where I invoke `std::find_if`. It takes const iterators to a std::vector. I'm now indirectly modifying objects through standard library functions that are modifying objects by multiple threads through const arguments.

Pretty sure this is a viral requirement and using any part of the STL can effectively taint your program if you're not careful.


Yes, access to mutable members should be synchronized in const methods.


> A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this

Consider the case where I invoke `std::find_if`. It takes const iterators to a std::vector. I'm now indirectly modifying objects through standard library functions that are modifying objects by multiple threads through const arguments. Using a mutex doesn't change the fact that my usage of the STL is now in undefined behavior land.

I think the only safe thing to do is to have a mutable mutex (there may be other parts of the legalese) but outside of that you may run into serious trouble at some point compilers become intelligent enough to enforce that undefined behavior. Realistically I doubt any compiler would ever enforce that legalese because of how const & mutable both are interpreted to mean "this variable is thread-safe" by everyone involved so a mutable variable that is synchronized in some way will always work fine.


Yup. As in everything, the INTENT is very important when judging someone's code.

Unfortunately, this makes it very easy to set up strawmen, then add a click-baity title for the perfect gift for "your side".


Using classes and objects for laziness is a hack to work around the limitations of OOP. In more functional languages, there are better ways to accomplish that. For example, in Scala, you can just use "lazy val", and in Haskell, everything's lazy by default.


Finally, the first real example. Every single one so far has been pretty weak.




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

Search: