Article View: comp.std.c++
Article #220Re: casting "const" to "non-const"
From: Joe Buck
Date: Wed, 01 Aug 1990 22:34
Date: Wed, 01 Aug 1990 22:34
39 lines
2212 bytes
2212 bytes
In article <GLENN.90Aug1145326@huxley.bitstream.com>,
glenn@bitstream.com (Glenn P. Parker) writes:
|> Until recently (after C++ invented it), C did not have "const". Therein
|> lies some confusion. I submit that it is absurd to put const in the same
|> category with other type names. Const is not a typename, it is a storage
|> qualifier, like "auto" and "static". You can't typecast from auto to
|> static, and I agree with Jim that you shouldn't be able to cast from const
|> to non-const.
The overloading system in C++ treats const as part of the type; there may be
int foo(char*);
int foo(const char*);
and these may be two different functions. const is part of the type; it's
different from static and auto.
I've had to write casts from const to non-const because the following situation
sometimes arises:
a) The writer of some library function declares an argument to be type char*,
although the string is not changed by the function;
b) I want to pass a const char* to it. I wouldn't want to see this forbidden.
I do think it's reasonable to give writers of optimizing compilers carte
blanche
to assume that a function that takes a pointer to const cannot under any
circumstances change the object. If you cheat and do it anyway by casting
or other tricks, the compiler is free to break your code; you deserve to lose.
Historically (from C) a cast says "trust me, I know what I'm doing". The same
should apply in C++. The programmer should be allowed to write any type of
cast, and the resulting program may be non-portable if he does.
--
Joe Buck
jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck
Technical Details
Message-ID:
<37922@ucbvax.BERKELEY.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!ucbvax!galileo.berkeley.edu!jbuckReferences:
<56159@microsoft.UUCP> <56163@microsoft.UUCP> <1913@ux.acs.umn.edu> <GLENN.90Aug1145326@huxley.bitstream.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!ucbvax!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.std.c++ Subject: Re: casting "const" to "non-const" Message-ID: <37922@ucbvax.BERKELEY.EDU> Date: 1 Aug 90 22:34:17 GMT References: <56159@microsoft.UUCP> <56163@microsoft.UUCP> <1913@ux.acs.umn.edu> <GLENN.90Aug1145326@huxley.bitstream.com> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Distribution: comp Lines: 39