const/immutable member functions

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 24 06:27:34 PST 2011


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.

-Steve


More information about the Digitalmars-d mailing list