Bug 3941 – quirks of overloading function templates complicate the new operator overloading
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-12T13:43:00Z
Last change time
2014-02-15T02:46:52Z
Keywords
rejects-valid
Assigned to
nobody
Creator
hoganmeier
Comments
Comment #0 by hoganmeier — 2010-03-12T13:43:48Z
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) );");
}
}
void main()
{
auto v = Vector2!(float)(0f,0f) + Vector2!(float)(1f,1f);
}
yields:
template instance opBinary!("+") matches more than one template declaration
This can be worked around with:
opBinary(string op, U:Vector2)(U v)
opBinary(string op, U:int)(U v)
Solutions could be:
(1) Something like
template fn(string op)
{
fn(args1) {...}
fn(args2) {...}
}
could work if a + b was rewritten as x.opBinary!("+").opBinary(b);
(2) the best solution would of course be to detect such function template overloads directly!
Comment #1 by andrej.mitrovich — 2012-01-21T17:56:57Z
Can't reproduce in 2.057, I guess it's fixed. Reopen if necessary.