What guarantees does D 'const' provide, compared to C++?

Mehrdad wfunction at hotmail.com
Fri Aug 17 14:33:26 PDT 2012


On Friday, 17 August 2012 at 21:25:31 UTC, Jesse Phillips wrote:
> On Friday, 17 August 2012 at 01:51:38 UTC, Mehrdad wrote:
>> If you did, then the code would be invalid, and the compiler 
>> could simply format your C: drive instead of modifying the 
>> object.
>
> This is probably the worst discussion point when people talk of 
> why undefined behavior is bad.

I recommend reading these (all three, not just the first one), if 
you haven't already:

http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html


> It is true in that you won't know what happens when in an
undefined state, but it is false in that, if it formats your C
drive then ~you'd have to be running Windows~, that would be
defined behavior and the spec would have said "Implementation
Defined"

No, you're completely missing the point.
"Implementation defined" and "undefined" are different terms, 
defined in the C++ standard. Go look them up. (I used to think 
like you as well, until I was corrected.)



> Which actually brings another point "Implementation Defined" is 
> then just as bad as Undefined because the compiler can do 
> whatever it wants! (And yes I know it is bad too).

I used to think like you too...

Implementation defined means "we don't tell you what it means, 
but the implementation will define it, and you can rely on 
getting sensible behavior out of it".

Undefined means "we don't tell you what it means, and we don't 
really expect the implementation to tell you, either, so unless 
the implementation tells you otherwise, if you run into it, your 
program will be meaningless".


More information about the Digitalmars-d mailing list