Overloading Assignment operator in template based class

842 views Asked by At

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.

1

There are 1 answers

1
Ionut On BEST ANSWER

Let's say you're working with normal classes, not templates. You have a class SomeType and you want to have an assignment operator for this class so you can assign objects of type OtherType to objects of this class. So something like this:

SomeType obj1;
OtherType obj2;
obj1 = obj;

For this to work you would write the assignment operator for SomeType like this:

SomeType& operator=(const OtherType& other)
{
    // implementation...

    return *this;
}

Converting this to templates, SomeType and OtherType are instantiations of the same template class but with different template parameters. In this case SomeType becomes fp_int<INT_BITS, FRAC_BITS> and OtherType becomes something like fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS>.

So your operator should look like this:

template <int DIFFERENT_INT_BITS, int DIFFERENT_FRAC_BITS>
fp_int<INT_BITS, FRAC_BITS>&
    operator =(fp_int<DIFFERENT_INT_BITS, DIFFERENT_FRAC_BITS> value)
{
    // proper implementation for an assignment operator
}

Compare the template parameters above to the ones in your example to see the difference. Basically you were trying to do the conversion in the wrong direction, this is why you were getting a compile error regarding type conversion.