Re:The Time Needed to Write “Effective Modern C++”
by nostrademons on Thursday April 23, 2015 @03:27AM
(#9425367)
Const-ness gives you a way to express a very common pattern in software, where an object is constructed piecemeal in one section of the code and then "frozen" and only const references get passed around from then on. If you were to express this at the class level, you'd need awkward circumlocutions like the Builder pattern, comments on methods, or friend member functions.
[ Parent
| on HN ]
Re:The Time Needed to Write “Effective Modern C++”
by pkolaczk on Thursday April 23, 2015 @03:47AM
(#9425409)
The builder pattern is the right way to go. It is stronger than const-references, because once the object is created, there is no way to unfreeze it. It guarantees that the object is truly const, no matter what. On the other hand, const keyword in C++ does not guarantee the object is really const. Your reference may be const, but something else might still have a non-const reference and mutate the object.
Re:The Time Needed to Write “Effective Modern C++”
by netheril96 on Thursday April 23, 2015 @04:45AM
(#9425521)
Most of the time `const` is only needed for the caller to ensure that the object won't get changed when passed to another function.
In addition, to enforce immutability in C++, you have to disable copy/move constructors and assignment operators, which removes much benefit of using values instead of references.
Re:The Time Needed to Write “Effective Modern C++”
by coliveira on Thursday April 23, 2015 @09:33AM
(#9426513)
> `const` is only needed for the caller to ensure that the object won't get changed when passed to another function.
But const cannot guarantee this, because it can be cast away so easily. This could be true just for your own code, but then it is you who is responsible for maintaining the immutability, not the compiler.
Re:The Time Needed to Write “Effective Modern C++”
by netheril96 on Thursday April 23, 2015 @10:27AM
(#9426883)
> because it can be cast away so easily
By this logic, none of the static type checks in C++ is of any use, since they can be cast away easily as well.
Re:The Time Needed to Write “Effective Modern C++”
by coliveira on Thursday April 23, 2015 @06:58PM
(#9430448)
Types exist to help you organize your code. Const (for classes) just promotes the misguided idea that you should design objects with mutable and immutable parts at the same time. The main objection to this is that your objects should be either value objects (therefore immutable) or reference objects. There is no need for const if you design your classes in this way. On the other hand, the const idea requires you to spread "const" everywhere you might want immutability, otherwise the compiler will fight you at each line of your code. Either way, it is a loss-loss proposition.
Re:The Time Needed to Write “Effective Modern C++”
by seabee on Thursday April 23, 2015 @06:41AM
(#9425815)
I see this sometimes in code at work. As soon as you need to stuff one of the 'const' objects into any kind of container you start requiring unique_ptrs everywhere, and then you start thinking how much easier your life would be if you wrote Haskell for a living instead.
Re:The Time Needed to Write “Effective Modern C++”
by coliveira on Thursday April 23, 2015 @09:30AM
(#9426494)
It that is your intent, then build an immutable class with some friends to initialize the object if necessary. The const thing is so overloaded in C++ that is means close to nothing in terms of intent, and it can be easy circumvented.