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

Mehrdad wfunction at hotmail.com
Thu Aug 16 20:11:49 PDT 2012


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.
Sorry, I'm a little confused at what you were illustrating here.


More information about the Digitalmars-d mailing list