Casting away const
Steven Schveighoffer
schveiguy at yahoo.com
Mon Aug 9 07:16:20 PDT 2010
On Mon, 09 Aug 2010 10:11:39 -0400, Don <nospam at nospam.com> wrote:
> Steven Schveighoffer wrote:
>> On Sun, 08 Aug 2010 17:56:25 -0400, simendsjo
>> <simen.endsjo at pandavre.com> wrote:
>>
>>> I'm totally new to the const/immutable thing, so this might be a naive
>>> question..
>>>
>>> The spec says:
>>> "modification after casting away const" => "undefined behavior"
>> I thought it was "you're on your own", not undefined behavior. The
>> former implies there is some "right" way to do this if you know more
>> about the data than the compiler, the latter implies that there is no
>> right way to cast away const. Am I wrong?
>> -Steve
>
> I think you're wrong. It's OK to cast away const, as long as you don't
> modify the thing which is supposed to be const.
> But if you modify it, there's no way the compiler can guarantee that
> your code will work. Anything could happen.
But then what is the point of casting away const? If you are not going to
modify it, then there is no reason to cast it away.
What about how Rebindable works? It doesn't exactly cast away const, it
uses a union, but the effect is the same. Is this OK simply because a
union is used?
Also, see my reply to bearophile, with hypothetical example.
-Steve
More information about the Digitalmars-d-learn
mailing list