From 3546602233951907847
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,d40995f71ce940ac
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1990-07-31 09:19:42 PST
Path: gmdzi!unido!mcsun!uunet!snorkelwacker!tut.cis.ohio-state.edu!usenet.ins.cwru.edu!eagle!news
From: fsset@bach.lerc.nasa.gov (Scott E. Townsend)
Newsgroups: comp.std.c++
Subject: Re: casting "const" to "non-const"
Message-ID: <1990Jul31.161942.1938@eagle.lerc.nasa.gov>
Date: 31 Jul 90 16:19:42 GMT
References: <56159@microsoft.UUCP> <56163@microsoft.UUCP> <1913@ux.acs.umn.edu>
Reply-To: fsset@bach.UUCP (Scott E. Townsend)
Organization: NASA/Lewis Research Center, Cleveland
Lines: 52
Posted: Tue Jul 31 17:19:42 1990

In article <1913@ux.acs.umn.edu> hopper@ux.acs.umn.edu (Nils  McCarthy) writes:
>In article <56163@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>>Proposed:
>>
>>That when a parameter [including the hidden "this" parameter] is declared 
>>"const", or when the object referenced by a parameter is declared "const",
>>than it is not legal to modify that which is declared "const" within the 
>>function. This is to say, the trick of casting to a non-const does not 
>>work on a parameter declared as "const."  Compilers can flag as an error 
>>these situations.
>>
>
>	I don't agree with the illegalization of this construct. I think it
>is against the spirit of the language to illegalize a cast.
>
>	In C, and C++ a cast is supposed to always work, no matter what
>object you're casting. I think that it should be legal for compilers to
>preform optimizations based on the information given by declaring a const
>parameter, but programmers should be allowed to cast if they feel it
>neccesary.
>
>Have fun,
>UUCP: rutgers!umn-cs!ux.acs.umn.edu!hopper   (Eric Hopper)
>     __                    /)                       /**********************/
>    / ')                  //                         * I went insane to   *
>   /  / ______  ____  o  //  __.  __  o ____. . _    * preserve my sanity *
>  (__/ / / / <_/ / <_<__//__(_/|_/ (_<_(_) (_/_/_)_  * for later.         *
>Internet:              />                            * -- Ford Prefect    *
>hopper@ux.acs.umn.edu </  #include <disclaimer.h>   /**********************/

I disagree with the statement "In C ... a cast is always supposed to work.."
In fact, it does not always work, although you are always allowed to try
without the compiler getting too upset.  For instance, casting a pointer
to a char as a pointer to double is not guaranteed to work, event if the
bit pattern at the address is a legal floating-point number.  Why? Because
some machines don't support arbitrary alignment of operands and many
compilers (rightfully IMHO) don't slow down the code with multiple fetches
to get around the alignment problem of the hardware.

I submit that const cast tricks are in a similar class to char/double
cast tricks -- they might work but are not guaranteed and shouldn't be
assumed to work.  You might even get a bus error trying to write to ROM
or a read-only peripheral register.  Let the optimizer do their thing without
having to worry about a buch of tricks.  If you don't want a const, don't
use a const!

--
------------------------------------------------------------------------
Scott Townsend               |   Phone: 216-433-8101
NASA Lewis Research Center   |   Mail Stop: 5-11
Cleveland, Ohio  44135       |   Email: fsset@bach.lerc.nasa.gov
------------------------------------------------------------------------


