What does C++ do better than D? - StackOverflow.com
Jonathan M Davis
jmdavisProg at gmx.com
Mon Aug 1 17:07:44 PDT 2011
> On 8/1/2011 4:36 PM, Jonathan M Davis wrote:
> > You lose logical const, which he has wanted to use in stuff that he's
> > been doing (games I think). He wants to lazy load values in his objects.
> > You can't do that with D's const. It pretty much means that that you
> > have to either eager load it or give up on const. It's totally doable,
> > but it forces you to change your design in a manner which you might not
> > like.
>
> I've talked to many people who use logical const extensively in C++, and
> really want it. If you dig down into what's happening, you'll find that
> logical const isn't actually supported by C++. It's a convention. There's
> simply nothing in the language that enforces that convention.
>
> The reason "logical const" does not work with const in D is because D
> actually enforces const semantics, and relies on that enforcement.
>
> Since logical const is a convention in C++ anyway, you can have logical
> const in D, too. Just put in a comment:
>
> struct S /* This struct is logical const */
> { ... }
>
> and then follow the convention just as you would with C++.
>
> Const in C++ is not powerful - it's simply a fraud - and the two get
> confused.
You do get some gains from const in C++, but it's pretty much all gained by
convention. It _is_ better than just comments in that the compiler will
complain if you break the convention, forcing you to explicitly break it (e.g.
via casting or mutable), but ultimately, it doesn't enforce much. I do believe
that it enforces enough that it's worth having, but it obviously doesn't
provide the guarantees that D's const does.
It seems to me that it would be possible to have logical const in D - at least
as far as lazy loading goes - if a feature were added to allow for a member
variable to be set the first time that it's accessed (you've essentially just
delayed the initialization of that particular variable). Obviously, it would
have to be disallowed when immutable is involved (or eagerly loaded in the
case of immutable). Syntactically, it would probably be something like
lazy int var = initializeFunc();
The initialization then takes place when var is first used rather than at
compile time.
Now, that could be very nasty to implement, and I'm not at all suggesting that
we consider such a feature right now (if anything, it would probable be a D3
feature), but it seems to me that it should be posible to add a mechanism to
the language specifically for the lazy loading of member variables which
worked even when the first time that the variable was accessed was in a const
function. Whether it's worth the time and effort it would take to implement
it, let alone the complication that it would add to the language, I don't
know. But it's at least theoretically possible.
The reality of the situation at the moment though is that D actually fully
enforces const (unlike C++), so it can't really even fake having logical const
(unlike C++), so you just can't use const in some situations in D where you
would in C++. It's certainly annoying sometimes, but in general, the gains far
outweigh the cost IMHO.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list