Head Const

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 17 11:03:25 PST 2016


On Wednesday, 17 February 2016 at 18:16:54 UTC, Ola Fosheim 
Grøstad wrote:
> On Wednesday, 17 February 2016 at 17:47:02 UTC, Jonathan M 
> Davis wrote:
>> definitely sucks. But interestingly, the more I've used D, the 
>> less happy I've been with C++'s const. It just has too many 
>> holes. In particular, the fact that it's not transitive is
>
> What you could consider in C++ is to fully encapsulate your 
> field values and use member functions to access them. Which is 
> considered good modelling anyway.
>
> So if you access a member as "get_child() const", you return a 
> const reference, which makes it transitive.

As soon as you start using pointers or containers or pointers or 
anything like that, pretty quickly, you can get situations like 
where the container is const, but its elements aren't. So, you 
may not be able to mutate the stuff that's directly a member of 
the class, you can still mutate stuff that it refers to or even 
owns. D doesn't allow that, which I think is a definite 
improvement. If you return a reference or pointer to a member 
variable from a const member function in D, it's const through 
and through with no holes, and I think that that is unequivocally 
a good thing.

Where things get annoying is the type of situation where you'd 
mark something mutable in C++, since those just don't work at all 
in D. You're forced to not use const or redesign what you're 
doing so that it doesn't need mutable (which usually means not 
using const).

- Jonathan M Davis


More information about the Digitalmars-d mailing list