copy must be const?!?

Dom DiSc dominikus at scherkl.de
Wed Jul 24 15:08:28 UTC 2024


In following code:
```d
int test(int x) { return --x; }
T test2(T)(T x) { return --x; }

void main()
{
    const int v = 3;
    writeln(test(v));
    writeln(test2(v)); // doesn't compile
}
```

test2 (just like test) works on a copy of x. Why is this copy 
const?!? If I copy a const or immutable object, most of the time 
I do so because I want to work with this value. Getting a const 
copy is never useful. If it were, why is it not const in test?

This also annoys me if I copy write protected files on windows - 
why on earth should the copy be write protected?

Is there a way to tell the compiler that it should discard 
"const" and "immutable" if it needs to create a copy?
Unqual!T doesn't work :-(


More information about the Digitalmars-d-learn mailing list