www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5158] New: Allow operator overloading on non-type template instances

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

           Summary: Allow operator overloading on non-type template
                    instances
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: simen.kjaras gmail.com


--- Comment #0 from Simen Kjaeraas <simen.kjaras gmail.com> 2010-11-03 04:52:02
PDT ---
template foo( alias A ) {
    void opAssign( typeof( A ) arg ) {
        A = arg;
    }
}

int a;
foo!a = 4;

The above currently does not work. Allowing this to compile and run would
enable the creation of throwaway types that do nothing but simple operator
overloading, allowing for e.g. reference tuple elements:

import std.typecons;
import std.typetuple;
import dranges.templates;
import dranges.typetuple;

template _( T... ) if ( allSatisfy!( isAlias, T ) ) {
    Tuple!( StaticMap!( TypeOf, T ) ) opAssign( Tuple!( StaticMap!( TypeOf, T )
) args ) {
        foreach ( i, e; T ) {
            e = args[i];
        }
        return args;
    }
}


unittest {
    int a = 1; b = 1;
    _!( a, b ) = tuple( b, a+b ); // Equivalent to _!( a, b ).opAssign( b, a+b
);
}

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2012-12-18
12:51:08 PST ---
This works:

template foo( alias A )
{
    struct Foo
    {
        void opAssign( typeof( A ) arg )
        {
            A = arg;
        }
    }

    enum foo = Foo();
}

void main()
{
    int a;
    foo!a = 4;
}

Note that changing 'enum foo' to 'auto foo' creates a segfault at runtime,
which might be an interesting unrelated bug.

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