xxxxxxxxxx
struct Foo
{
int i;
float f;
auto opEquals()(auto ref const(typeof(this)) rhs)
{
import std.math : approxEqual, isNaN;
import std.traits : isFloatingPoint, isIntegral;
static foreach(i; 0..this.tupleof.length)
{
{
alias FType = typeof(this.tupleof[i]);
// a field of this structure
auto tf = this.tupleof[i];
// a field of other structure
auto of = rhs.tupleof[i];
static if (isFloatingPoint!FType)
{
if (!tf.isNaN || !of.isNaN)
{
if (!approxEqual(tf, of))
return false;
}
}
else static if (isIntegral!FType)
{
if (tf != of)
return false;
}
else
static assert (0);
}
}
return true;
}
}
void main(string[] args)
{
Foo f1, f2;
assert (f1 == f2);
}