About foreach loops

Timon Gehr timon.gehr at gmx.ch
Fri Jun 17 07:57:01 PDT 2011


Jesse Phillips wrote:
> On Wed, 15 Jun 2011 16:40:24 -0400, bearophile wrote:
>
>> Isn't it undefined in D to modify a const?
>
> Yes, but unlike what every redditer is say, that doesn't mean the
> compiler can do whatever it wants. It means you can't define its
> behavior. Go ahead try.
> [snip.]

How do "cannot do what it wants" and "the behavior cannot be defined" not
contradict each other?

It may work as you would expect in some cases but not in others. "The compiler can
do what it wants". The behavior will depend on what the compiler wants. The way
out would be to just define the behavior of casting away const and mutating the
memory to do the expected thing in case it is not typed as immutable somewhere in
the program. Would that kill any opportunities for optimization? I don't think so.


Timon


More information about the Digitalmars-d mailing list