how to properly overload function templates?
Trass3r
un at known.com
Thu Mar 11 16:15:37 PST 2010
I stumbled across this while playing with operator overloading. Since they
are now function templates, this becomes an issue.
struct Vector2(T)
{
T x;
T y;
/// element-wise operations, +, -,
Vector2 opBinary(string op)(ref Vector2 v)
{
mixin("return Vector2!(T)( cast(T)(x " ~ op ~ " v.x), cast(T)(y " ~ op ~
" v.y) );");
}
/// operation with scalar
Vector2 opBinary(string op)(int i)
{
mixin("return Vector2!(T) ( cast(T)(x " ~ op ~ " i), cast(T)(y " ~ op ~
" i) );");
}
}
This yields:
template instance opBinary!("+") matches more than one template declaration
Of course this can be circumvented by using
opBinary(string op, U:Vector2)(U v)
opBinary(string op, U:int)(U v)
But is this how it's supposed to be done? Couldn't the compiler detect
that itself?
More information about the Digitalmars-d-learn
mailing list