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

Jesse Phillips Jessekphillips+D at gmail.com
Thu Aug 16 16:18:04 PDT 2012


On Thursday, 16 August 2012 at 22:14:31 UTC, Mehrdad wrote:
> Something I'm having trouble undertanding/remembering (sorry, 
> you've probaby already explained it a billion times)...
>
> I remember being told many times that D's 'const' provides 
> stronger guarantees than C++'s 'const'.

> If it's the former, is there some example piece of code in each 
> language, for comparison, that shows how the compiler can infer 
> more from D's const than C++'s?

Note that stronger guarentees does not translate to inferences 
done by the compiler.

> If so, it might be worth posting something like that on the 
> website.

I believe there is an article which speaks to const, but the 
inference benefits come from immutability and pure functions. The 
const page does have a comparison table at the end:

http://dlang.org/const3.html

The main thing given is transitivity. The compiler will guarantee 
you aren't changing data that is const.

On the note of casting away const, I don't believe that is the 
operation which is undefined, however modifying const is 
undefined as it could be pointing to immutable data.


More information about the Digitalmars-d mailing list