Bug 5158 – Allow operator overloading on non-type template instances

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-03T04:52:02Z
Last change time
2024-12-13T17:54:03Z
Assigned to
No Owner
Creator
Simen Kjaeraas
Moved to GitHub: dmd#18314 →

Comments

Comment #0 by simen.kjaras — 2010-11-03T04:52:02Z
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 ); }
Comment #1 by andrej.mitrovich — 2012-12-18T12:51:08Z
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.
Comment #2 by robert.schadek — 2024-12-13T17:54:03Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18314 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB