www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3941] New: quirks of overloading function templates impede the new operator overloading

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3941

           Summary: quirks of overloading function templates impede the
                    new operator overloading
           Product: D
           Version: 2.041
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: mrmocool gmx.de



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!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 12 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3941


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich gmail.com
         Resolution|                            |FIXED



17:56:57 PST ---
Can't reproduce in 2.057, I guess it's fixed. Reopen if necessary.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 21 2012