const debacle

Steven Schveighoffer schveiguy at yahoo.com
Sun Mar 23 05:54:56 PDT 2008


"Janice Caron" wrote in message
> On 23/03/2008, Janice Caron wrote:
>> I don't think it's a broken concept. In fact, I think that what
>>  Stephen wants is completely reasonable and useful.
>
> Stephen, I think this does the trick.
>
>    T f(T : const(char)[])(T a)
>    {
>        return a[1..$];
>    }
>
>    private alias f!(const(char)[]) dummy;
>
> The alias forces an instantiation for T == const(char)[], and if that
> compiles, then the function does not modify its input.

This is similar to what I was saying, but I appreciate the actual method of 
ensuring to the developer of f that the compiler has checked for 
const-correctness.  This, however, does nothing for the user of f, who must 
still check the source code, or instantiate a const version himself (which 
even then does not guarantee as you have said, static if could get around 
that).

However, I have lessened my belief that this ommission is bad, as there are 
solutions that get very close to optimal.  I still think it would be nice to 
have a way to specify this kind of contract, but if it never happens, it's 
not going to block my work.

-Steve 





More information about the Digitalmars-d mailing list