const debacle

Walter Bright newshound1 at digitalmars.com
Sun Mar 23 11:35:30 PDT 2008


Janice Caron wrote:
> Fortunately, I think I've already solved the problem, a few posts up.
> The solution in this case is:
> 
>    T strchr(T : const(char)[])(T s, char c)
>    {
>         int n = s.find(c);
>         return n == -1 ? null : s[n..$];
>    }
> 
> I don't see that there is anything fundamentally broken about that.

It isn't broken because the return type T is const.

> T
> is any type which will implicitly cast to const(char)[]; you have
> compile-time checking that s doesn't get modified within the function;
> s /can/ be modified via it's return value /if s is not const/. This is
> exactly what's being requested, is it not? Where's the problem with
> it?

The problem with that strchr is it doesn't address Steven's issue - if 
the input is const, the output is, too. strchr is otherwise correct.



More information about the Digitalmars-d mailing list