I tried your class, iMalc, and everything seems to work like it does for floats or doubles, except from the ?: operator, just like for the class I defined.
Also, I had to change the return value for the conversion operator to value, since it returned retVal which was undefined. And to generate a NaN, on all machines that support IEEE floating point there is a macro defined in <math.h> called NAN that will do that (maybe that is what you would use in practice).
As of the problem with the ?: operator: I have the following code
Code:
ftype a = MIN(b - c, d);
where b, c and d are ftype objects, where ftype is defined as
Code:
typedef nanInit<float> ftype
and MIN is a macro defined as
Code:
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
However, the code won't compile, and the message I'm getting from the compiler is:
Code:
error: operands to ?: have different types 'double' and 'pftype'
Converting d to float works but is bad, because the definition of ftype may change.
I may instead convert b - c back to ftype, so I have
Code:
ftype a = MIN(ftype(b - c), d);
which is better, because that code is independent of the definition of ftype.
However, my project is quite small and I have already had to change on 21 places in the code just becase I changed the definition of ftype from float to nanInit<float>. For larger projects, there could be a lot more work than this that would be necessary just for changing the typedef of ftype (or a corresponding typename), which kind of feels unnecessary. Is there really no way to get around this problem?