FixedPoint
FixedPoint copied to clipboard
Initialization of FixedPoint<31,31> gives error.
The error is in the the constructor not being able to be overloaded.
Steps for reproduction : Add FixedPoint<31,31> in the examples.cpp and compile.
It seems the problem is that when the native integer type for the fixed-point number is the same as the type int
, for the purposes of overload resolution, then there are some ambiguities about which constructor overload is represented.
One solution is to use static functions to construct the fixed points explicitly from an integer or float. For backwards compatibility we could leave in constructor from "double", although the performance may be sacrificed for users who don't change their client code to use the static functions. This is the implementation I propose for this:
/// Create a fixed-point with equivalent value to a given integer
/** For example in 4.12 fixed-point, the number "2" is 0010.000000000000 */
static ThisType fromInt(IntType value) { return ThisType(RawValue(value << FRAC_BITS)); }
/// Create a fixed-point with equivalent value to a given double-precision floating point
/** For example in 4.12 fixed-point, the number "2.0" is 0010.000000000000 */
FixedPoint(double value) : raw_((RawType)(value * (1 << FRAC_BITS))) { }
/// Create a fixed-point with equivalent value to a given single-precision floating point
/** For example in 4.12 fixed-point, the number "2.0" is 0010.000000000000 */
static ThisType fromFloat(float value) { return ThisType(RawType(value * (1 << FRAC_BITS))); }
Fixed that using by commenting the IntType
constructor.
But these now gives rise to a new problem. The FixedPoint<60,4>
defined in the compatibility shows a conversion from long int
to FixedPoint<60,4>
is ambiguous error.
I think it is an error as the we removed the IntType
Constructor and hence it supports only native int
.