On Wednesday, 22 August 2018 at 11:58:25 UTC, XavierAP wrote:
I've been trying some things to template operator overloads.
The reason is that I want very similar code for different
types, but I can't use polymorphism, as they're structs rather
than classes. Perhaps this choice is not as advantageous as I
think, and I may change this design from structs to classes, or
else the code duplication would be small and never subject to
change. But now I'm just trying for the sake of learning to
find out what works or not in terms of templated operator
overloading, and whether the reason something doesn't work is
by design and if mentioned in the specification, or just an
arbitraty result of some unspecified parsing/lowering step
order, or it depends on the compiler (I'm using dmd).
[...]
"void opOpAssign(string op, T)(ref Tthis, const ref T x)" looks
like the wrong signature for opOpAssign. THink it needs to be:
void opOpAssign(string op, T)(const ref T x)
Then:
mixin template operator!
{
void opOpAssign(string op, T)(const ref T x)
{
writeln(this, op, x);
}
}
struct S1
{
mixin operator;
}
struct S2
{
mixin operator;
}
Cheers,
- Ali