A problem with const

bearophile bearophileHUGS at lycos.com
Thu Jul 14 02:31:17 PDT 2011


A D2 program:


T[] foo(T)(const T[] x) {
    //static assert(is(U == int)); // false
    static assert(is(T == const(int)));
    return new T[1];
}
U[] bar(U)(const U[] y) {
    static assert(is(U == int));
    return foo(y);
}
void main() {
    bar([1]);
}


DMD 2.054 gives:
test.d(8): Error: cannot implicitly convert expression (foo(y)) of type const(int)[] to int[]
test.d(11): Error: template instance test.bar!(int) error instantiating


Do you know why T of foo is const(int) instead of int? Isn't the "const" of foo(T)(const T[] x) de-structuring the const nature of x? Is is possible to change/"fix" this?

This causes me problems because many of my functions have "in" arguments. When they call each other they don't compile, as in this example (here I have used "const" arguments just for clarity).


I have had to use code like this:

Unqual![] foo(T)(const T[] x) {
    return new Unqual!T[1];
}
U[] bar(U)(const U[] y) {
    return foo(y);
}
void main() {
    bar([1]);
}

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list