opBinary failes with templates
Asger Dam Hoedt
asgerhoedt at gmail.com
Wed Jul 27 12:48:24 PDT 2011
Hey
I've very recently started playing around with D, but I seem to have hit my
head against a bug in dmd. I want to create a Vector class templated with
its dimension. This works fine until I try to overload the + operator for my
vector and use it. I then get the error
Error: incompatible types for ((vec) + (vec)): 'Vector!(3)' and 'Vector!(3)'
I've boiled it down to a small example
struct Vector(int D) {
float es[D];
public:
this(immutable float x, immutable float y, immutable float z) {
es[0] = x; es[1] = y; es[2] = z;
}
Vector!(D) opBinary(string s)(immutable Vector!(D) rhs) if (s == "+") {
Vector!(D) ret;
for(int i = 0; i < D; ++i)
ret.es[i] = es[i] + rhs.es[i];
return ret;
}
}
alias Vector!(3) Vector3;
void main() {
Vector3 vec = Vector3(0,1,2);
vec = vec.opBinary!("+")(vec); // This works fine
vec = vec + vec; // This line fails miserably
}
If I replace the template argument in for the argument rhs with 3, then
everything works, but that's not really a nice solution :)
Is it me trying to use templates in a way they aren't meant to be used or is
this a bug in dmd? If it is a bug, does anyone have an idea how to solve
this? I wouldn't mind fixing it in dmd myself, I just need some guidelines.
/papaboo
--
I confess
---
>From the desk of Dr. John Zoidberg, M.D.
(\/)(',,,')(\/)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110727/dcb9bd04/attachment.html>
More information about the Digitalmars-d
mailing list