const functions in D 2.0

Nathan Reed nathaniel.reed at gmail.com
Mon Nov 5 18:38:47 PST 2007


Steven Schveighoffer wrote:
> "Jarrett Billingsley" wrote
>> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message 
>> news:fgoak7$24ao$1 at digitalmars.com...
>>> How does one declare const/invariant member functions in D 2.0 that don't 
>>> return void?  If it's just prefixing, how does one declare 
>>> const/invariant functions that return const/invariant references?
>>>
>>> The web site isn't very helpful on this matter.
>>>
>>> -Steve
>>>
>> const Bar foo()
>> {
>>    // returns a non-const Bar, but can't modify this.
>> }
>>
>> const const(Bar) foo()
>> {
>>    // returns a const(Bar).
>> }
>>
>> At least I'm pretty sure.
>>
> 
> Experimentation proves you are correct.  Thanks!
> 
> This really should be more prominent on the web site.

Actually, this really should be changed in the language...it's kind of 
silly IMHO that const Bar foo() and const(Bar) foo() mean different things.

How about putting the const or invariant of a function at the end of the 
function header like C++ does?

     Bar foo () const    // ret-val is not const, 'this' is

I don't know what was so wrong with this aspect of C++ syntax that it 
had be replaced with the confusing syntax we have now.

Thanks,
Nathan Reed


More information about the Digitalmars-d-learn mailing list