I am writing a library to support a type of integers which have two template parameters INT_BITS
and FRAC_BITS
. I was successful in writing a convert function to convert different class types from one to another [ which vary in values of INT_BITS
and FRAC_BITS
]. But when I try to use it in the overloading of assignment operator it doesn't work. Please suggest me a way to implement it. I have gone through links here here and here , but none of the solution seems to be working.
The Class definition :
template<int INT_BITS, int FRAC_BITS>
struct fp_int
{
public:
static const int BIT_LENGTH = INT_BITS + FRAC_BITS;
static const int FRAC_BITS_LENGTH = FRAC_BITS;
private:
ValueType stored_val;
};
The convert function definition :
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW> convert() const
{
typedef typename fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::ValueType TargetValueType;
return fp_int<INT_BITS_NEW, FRAC_BITS_NEW>::createRaw(
CONVERT_FIXED_POINT<
ValueType,
TargetValueType,
(FRAC_BITS_NEW - FRAC_BITS),
(FRAC_BITS_NEW > FRAC_BITS)
>:: exec(stored_val));
}
The operator definition goes as :
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
fp_int<INT_BITS_NEW, FRAC_BITS_NEW>
operator =(fp_int<INT_BITS,FRAC_BITS> value) const
{
fp_int<INT_BITS_NEW,FRAC_BITS_NEW> a = value.convert<INT_BITS_NEW,FRAC_BITS_NEW>();
return a;
}
When I try this it works :
fp_int<8,8> a = 12.4;
fp_int<4,4> b = a.convert<4,4>();
But when I attempt this it shows type conversion error:
fp_int<8,8> a = 12.4;
fp_int<4,4> b;
b = a;
Please tell me where I am going wrong.