www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template classes discriminated in receiving invariant arguments

reply mumba <qniol o2.pl> writes:
Some strange stuff with templates and invariant-argument method version
overload happens. Take a look:

template Template(T, int n) {
    class Class {
        void foo(T[n] array) {}
        void foo(invariant(T[n]) array) {}
    }
}

class Class {
    void foo(int[3] array) {}
    void foo(invariant(int[3]) array) {}
}

int main() {
    Template!(int, 3).Class fromTemplate = new Template!(int, 3).Class;
    Class normal = new Class;

    invariant(int[3]) array = [0, 1, 2];

    normal.foo(array); //works fine calling void foo(invariant(int[3])) version
    fromTemplate.foo(array); //here is the error

    return 0;
}

And the error is following:
template.d(20): function template.Template!(int,3).Class.foo called with
argument types:
	(invariant(int[3u]))
matches both:
	template.Template!(int,3).Class.foo(int[3u])
and:
	template.Template!(int,3).Class.foo(int[3u])


Uff, that was crazy message. Let's forget about the fact of omiting invariant
keyword by the template... We just created two functions with the same
signatures in the same class!

Any ideas?

cheers
Oct 05 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
You just discovered a bug:

import std.stdio;

class Foo(T, int n) {
     void bar(T[n] array)
     {
         array[0] = 42;
     }
}

void main() {
     auto foo = new Foo!(int, 3);

     invariant(int[3]) array = [0, 1, 2];
	
     //array[0] = 42; Error: array[0] isn't mutable

     writefln(array); // [0 1 2]
     foo.bar(array);
     writefln(array); // [42 1 2]
}
Oct 05 2008