osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Lies in education [was Re: The "loop and a half"]


On 12/10/2017 06:39, Grant Edwards wrote:
> On 2017-10-11, Gregory Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Neil Cerutti wrote:
>>> I dig
>>> const qualifiers, even though I'm comletely fine with their
>>> absence from Python.
>>
>> Out of curiosity, do you have any insights into why you
>> like them in C++, if you don't miss them in Python?
> 
> I like them in C because it allows the linker to place them in ROM
> with the code.  It also _sometimes_ provides useful diagnostics when
> you pass a pointer to something which shouldn't be modified to
> something that is going to try to modify it.

It's one of these features that on paper sound good. In practice, it's 
just useless extra clutter. It's used to:

(1) Define named constants; except (in C) they can't be used like 
constant expressions, you can take their addresses, and accidentally or 
maliciously change their values.

(2) Declare data to be put into read-only memory as you say. That's fine 
with a 'const int * p', but what about a 'int * const p'? (Or is it the 
other way around? Whatever...). Or any other hybrid type where some 
elements are const and some aren't.

(3) Pass data that is supposed to be read-only, but with any even 
slightly complex type, it won't work (the head of a linked list for 
example, where the node elements need to be writeable). It gives a false 
sense of security.

-- 
bartc