const/immutable member functions

Jonathan M Davis jmdavisProg at gmx.com
Mon Jan 24 06:39:16 PST 2011


On Monday 24 January 2011 06:27:34 Steven Schveighoffer wrote:
> On Mon, 24 Jan 2011 09:20:17 -0500, Jonathan M Davis <jmdavisProg at gmx.com>
> 
> wrote:
> > On Monday 24 January 2011 05:56:49 Trass3r wrote:
> >> class F
> >> {
> >> const Foo bar();
> >> }
> >> 
> >> Isn't this ambiguous? "returns a const Foo object" vs. "is a const
> >> member function that returns a Foo object"?
> > 
> > When using const or immutable in a function signature, it _always_
> > applies to
> > the function, unless you use parens to say otherwise.
> > 
> > const Foo bar(); //const function
> > Foo bar() const; //const function
> > immutable Foo bar(); //immutable function
> > Foo bar() immutable; //immutable function
> > const(Foo) bar(); //mutable function with const return value
> > const(Foo) bar() const; //const function with const return value
> > immutable(Foo) bar(); //mutable function with immutable return value
> > immutable(Foo) bor() immutable; //immutable function with immutable
> > return value
> 
> You forget my favorite:
> 
> const const Foo bar(); // const function returning const Foo.

That works? I would have thought that it still would have required the parens.

Regardless, const and immutable on functions is not the cleanest corner of D. 
It's fairly easily understood and mostly doesn't result in bugs because marking 
the wrong thing as const usually results in a compilation error somewhere, but 
still, it would be nice if const and immutable had to go on the right-hand side 
like they do in C++.

- Jonathan M Davis


More information about the Digitalmars-d mailing list