Why do "const inout" and "const inout shared" exist?
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jul 2 18:15:47 PDT 2017
On 7/2/2017 5:46 AM, Shachar Shemesh wrote:
> Second, there are optimizations that can take place over "const" that cannot
> over "shared const" even assuming aliasing (such as if the compiler knows no
> other pointer was changed between two accesses).
>
> The last point is that assuming no pointer aliasing is a fairly common
> optimization to take in C and C++, simply because of the huge performance gains
> it provides. It is so huge that it is sometimes turned on by default despite the
> fact it changes language semantics. It would be a pity to block any potential to
> have it in D.
Pointer aliasing is indeed a big deal for optimization.
But that really has nothing to do with const in C++. The trouble with C++ const
is you can legally cast it away in C++. Chandler Carruth (LLVM optimizer) told
me that const was basically ignored by the optimizer because of that.
D const is different in that the compiler is allowed to generate code as if
const was never cast to immutable. Such casts are also not allowed in @safe code.
More information about the Digitalmars-d
mailing list