const

Bill Baxter dnewsgroup at billbaxter.com
Mon Mar 31 13:18:36 PDT 2008


Steven Schveighoffer wrote:
> "Walter Bright" wrote
>> I understand what you're saying. It's simply finding a method of 
>> transferring the 'constness' of the function argument to the function 
>> return type, without changing the contents of the function.
>>
>> Note that this is *not* a problem with const or invariant, it's a 
>> notational problem.
>>
>> This is currently done in C++ (and D) by writing each function twice. 
>> Obviously, that is hardly ideal.
>>
>> There have been some proposals to deal with it, but the current front 
>> runner is one Andrei came up with:
>>
>> typeof(a) foo(const(T) a) { ... }
> 
> What if the return can come from multiple inputs?  Such as the min/max 
> function:
> 
> typeof(a or b?) min(const(T) a, const(T) b) {...}
> 

And also what about the case of returning a member of a struct?

     typeof(Bar?) getBar(const(Foo) f) {  return f.bar; }

My problem with these new declarations is just that there are so many 
dang parentheses in the function signatures that it's hard to see what's 
what any more.

--bb



More information about the Digitalmars-d mailing list