const debacle

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 21 10:55:50 PDT 2008


"BCS" wrote
> Steven Schveighoffer wrote:
>> This issue was just brought to my attention, and I wonder if the other 
>> const-proponents have a good solution for this.  This is going to be very 
>> tough to deal with.
>>
>> How do you declare a function that takes an array, is not allowed to 
>> change the array, but returns a slice into the argument array, and the 
>> return type matches the argument type.
>>
>> For example, if I pass a mutable array to this function, I want it to 
>> pass me back a mutable slice into the array, but I also want a guarantee 
>> that the function will not modify the array.  I can't declare the 
>> argument is const because then the return value must also be const, as it 
>> is a slice of the original array.
>>
>> How does one solve this problem?
>>
>> -Steve
>
> IIRC Walter has something planed for this. Something to do with a "return" 
> const type. It would do just that; "the function's return type has the 
> same constness as one of it's arguments."
>
> In short: Not yet.

I remember that idea.  I'm concerned however that in this mode, the compiler 
will not check the function itself for const-correctness (i.e. ensure it 
doesn't modify the input) because the argument will not be const.  See my 
reply to Walter for my example.  I want a way to specify "the function's 
return type has the same constness as one of it's arguments, and it promises 
not to modify that argument, and the compiler made sure of that."

-Steve 





More information about the Digitalmars-d mailing list