Meaning of const

bearophile bearophileHUGS at
Tue Jan 24 17:01:41 PST 2012

Jonathan M Davis:

> Now, the confusing part is the fact that unlike C++, D allows you to put the 
> const for making the function on the _left-hand_ side of the function (C++ 
> only lets you put it on the right). This is to increase consistency between 
> modifers (public, override, pure, etc.) - they _all_ can go on both the right 
> and left (which is very unfortunate in the case of const and immutable IMHO). 
> That means that const (and immutable) always modify the function rather than 
> the return value unless you use parens.

Some time ago we have discussed this topic in an enhancement request in Bugzilla. The idea was to disallow code like:

struct Foo {
    int x;
    const const(int) bar() {
        return 0;
void main() {}

and require the struct "const", "immutable" to be only on the right if present.
Walter shot this idea down for consistency. But I generally don't want consistency when it's just a potential source of confusion for the programmer :-)


More information about the Digitalmars-d-learn mailing list