const and immutable values, D vs C++?
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Thu Dec 5 08:06:07 UTC 2019
I haven't used const much in the past because I got burned on
transitive const, so I managed to confuse myself. I am not
really interested in const/immutabel references here, only
values. Seems like there is no reason to ever use const values,
except when the value may contain a pointer to something
non-immutable?
The only difference, as far as I can tell, is if the value type
has pointers to other objects, immutable requires them to be to
immutable objects. Which is a bit odd when you think of it. It
basically means that you cannot put pointers to memory registers
in ROM in a type safe manner... :-/ . That is a very odd
restriction, because that is something you might want to do...
Seems to me that immutable should allow pointers to mutable
memory and that const really shouldn't have been applicable to
values, but only to pointers.
Anyway seems the rule of thumb would be:
- always use immutable on values
- except when the value might contain a pointer to non-immutable
Hm.
More information about the Digitalmars-d-learn
mailing list