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