const/immutable member functions

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 24 07:03:01 PST 2011


On Mon, 24 Jan 2011 09:39:16 -0500, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> 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.

I could have sworn it does, but on testing (which I probably should have  
done), it fails (even back on 2.033) with:

redundant storage class const

Sorry for the noise, I still find the const const(Foo) bar() very  
confusing to read.

-Steve


More information about the Digitalmars-d mailing list