digitalmars.D.learn - template classes discriminated in receiving invariant arguments
- mumba (29/29) Oct 05 2008 Some strange stuff with templates and invariant-argument method version ...
- Denis Koroskin (17/17) Oct 05 2008 You just discovered a bug:
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
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








"Denis Koroskin" <2korden gmail.com>