const debacle

Walter Bright newshound1 at digitalmars.com
Sat Mar 22 13:27:46 PDT 2008


Steven Schveighoffer wrote:
> I want to specify that 
> the function does not change the input buffer, and returns the same type as 
> passed in (without duping the input).

Look at it another way. You want to declare a filter that does not 
change the input, but can pass it along to another function that will. I 
  suspect this might be a fundamentally broken concept:

    T[] f(const T[]);
    void g(T[]);

    ...

    T[] t;
    g(f(t));

What's happened here? Although f() promised it wouldn't modify t, it got 
modified anyway, because f() passed t to g() which modified it. Far from 
being a weakness in D's const regime, I think the fact that such cannot 
be implemented without casting away const at some point shows that the 
const system is sound, and trying to implement such a filter is unsound.



More information about the Digitalmars-d mailing list