What guarantees does D 'const' provide, compared to C++?
Jesse Phillips
jessekphillips+D at gmail.com
Fri Aug 17 21:03:37 PDT 2012
On Friday, 17 August 2012 at 21:33:28 UTC, Mehrdad wrote:
> 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
He did not make his case on what of undefined behavior allows him
to format your hard drive. Instead it just gives some good
examples for what I am talking about.
void contains_null_check(int *P) {
int dead = *P; // Compiler sees dereference
if (P == 0) // Dereference indicates that you can't reach here
if null
return;
*P = 4;
}
At no point does the compiler know you have enacted undefined
behavior. Why? because does not have the information and is not
permitted to insert extra information that when P is null do...
>> 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.)
I am not missing the point, though it seems there is also
"Unspecified"
http://stackoverflow.com/a/4105123/34435
I am in agreement that the behavior of executing the code could
be a formating of the hard drive. However I do not agree that it
is the compiler which can cause this to happen an still conform
to the specification. There are other aspects to the
specification that would restrict the compilers ability to insert
arbitrary code. Undefined behavior is usually identifiable at
runtime, the compiler only has compile time information.
More information about the Digitalmars-d
mailing list