Re: auto const promotion question
adrian.hawryluk-at-gmail.com@nospam.com (Adrian Hawryluk) wrote
(abridged):
I have a question in regards to auto const promotion. When a class
object gets const promoted, all aggregate parts of the class get
promoted as well. However, anything pointed to outside of the class do
not get const promoted. I'm assuming this originally was because of C
and the wish to keep it a subset of C++.
I'm pretty sure it was C++ that first introduced const and the rules for
const propagation.
The reason is that pointers are used to express a variety of
relationships, and const should be propagated for some and not others. The
compiler can't guess what kind of relationship the programmer intends.
As a guideline, where the relation is "part of", const should be
propagated. For example, if a dog is const then the dog's tail is also
const because the tail is part of the dog. And vice-versa.
For other relations it's not so clear. For example, in
const std::pair<Dog *, Tail *> pair;
it might make sense for the pair to be const - meaning a dog is always
associated with the same tail - even if the things being related are not.
As a matter of physics and CPU design, it's possible for a piece of memory
that you cannot write to, to have a pointer to memory that you can write
to. To represent this you need non-propagating const.
And I expect that is where the C++ default comes from. Const propagates as
if it were "physical const".
However, I am designing a template library that will manage memory
pointers and while I am at it, I wish to know if anyone thinks that it
would be a good idea to plug this const promotion 'problem'.
It depends on what your pointers represent. If they only represent "part
of" relations, or variants there-of, then const propagation makes sense.
If you want them to be able to express any kind of relation than they
should behave like normal pointers.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]