c++ - Overloading Assignment operator in template based class -


i writing library support type of integers have 2 template parameters int_bits , frac_bits. successful in writing convert function convert different class types 1 [ vary in values of int_bits , frac_bits ]. when try use in overloading of assignment operator doesn't work. please suggest me way implement it. have gone through links here here , here , none of solution seems 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 :

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> = value.convert<int_bits_new,frac_bits_new>();      return a; } 

when try works :

fp_int<8,8> = 12.4; fp_int<4,4> b = a.convert<4,4>(); 

but when attempt shows type conversion error:

fp_int<8,8> = 12.4; fp_int<4,4> b; b = a; 

please tell me going wrong.

let's you're working normal classes, not templates. have class sometype , want have assignment operator class can assign objects of type othertype objects of class. this:

sometype obj1; othertype obj2; obj1 = obj; 

for work write assignment operator sometype this:

sometype& operator=(const othertype& other) {     // implementation...      return *this; } 

converting templates, sometype , othertype instantiations of same template class different template parameters. in case sometype becomes fp_int<int_bits, frac_bits> , othertype becomes fp_int<different_int_bits, different_frac_bits>.

so operator should 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 assignment operator } 

compare template parameters above ones in example see difference. trying conversion in wrong direction, why getting compile error regarding type conversion.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -