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

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 16 19:25:11 PDT 2012


On Friday, August 17, 2012 04:12:10 Mehrdad wrote:
> On Friday, 17 August 2012 at 02:02:51 UTC, Jonathan M Davis wrote:
> > Because there are plenty of functions which take mutable
> > objects but don't actually alter them - particularly when
> > interacting with C code.
> 
> Ah, so that explains that, thanks.
> 
> So to clarify, modifying a mutable object through casting away a
> const reference is undefined in D, but valid in C++.
> 
> Now the only question is what guarantees that actually gives you
> that the compiler can use for optimization (aliasing issue
> above^).

Yeah. It's more than C++, but it's still pretty limited without pure, and if
even with pure, the optimizations can still be pretty limited. Immutable gives
a _lot_ more. And while there's definitely optimizations to  be had with const,
I suspect that dmd doesn't exploit them very well at this point. Other issues
have had far higher priority than eking out every last bit of performance that
we can. So, regardless of what D's current performance is, I think that it's
pretty much a guarantee that it will improve as D matures.

- Jonathan M Davis


More information about the Digitalmars-d mailing list