Mehrdad | 17 Aug 2012 06:24
Picon
Favicon

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

On Friday, 17 August 2012 at 04:17:05 UTC, Jonathan M Davis wrote:
> On Friday, August 17, 2012 05:11:49 Mehrdad wrote:
>> On Friday, 17 August 2012 at 02:49:45 UTC, Jonathan M Davis 
>> wrote:
>> > But take this code for example:
>> > 
>> > auto i = new int;
>> > *i = 5;
>> > const c = i;
>> > writeln(c);
>> > func(c); //obviously takes const or it wouldn't compile
>> > writeln(c);
>> > 
>> > The compiler _knows_ that c is the same before and after the
>> > call to func, because it knows that no other references to 
>> > that
>> > data can exist.
>> 
>> Is there any reason why your example didn't just say
>> 
>> > const(int*) c = null;
>> > writeln(c);
>> > func(c);
>> > writeln(c);
>> 
>> i.e. What was the point of 'i' there?
>> And why can't a C++ compiler do the same thing?
>> 'c' is a const object, so if C++ code was to modify it, it 
>> would
>> be undefined behavior, just like in D.
(Continue reading)


Gmane