Compilation problem

Marc Glisse marc.glisse at inria.fr
Sun Feb 26 19:55:25 CET 2023


GMPXX uses expression templates, so val1 - val2 has a type "subtraction of 
2 numbers", and that's what is deduced for T. Clearly -eVal has a 
different type ("negation of a subtraction of 2 numbers"), and converting 
between expression types makes no sense in general.

As explained in 
https://gmplib.org/manual/C_002b_002b-Interface-Limitations , the use of 
expression templates in gmpxx means that users should limit their use of 
type deduction.

(for this particular function, gmpxx already provides abs())

On Sun, 26 Feb 2023, Mathieu Dutour wrote:

> Hello,
>
> I have some compilation error with following code:
> ----------
> #include "gmpxx.h"
>
> template <typename T> T T_abs(T const &eVal) {
>  if (eVal > 0)
>    return eVal;
>  T fVal = -eVal;
>  return fVal;
> }
>
> int main(int argc, char *argv[]) {
>  using T = mpq_class;
>  T val1 = argc;
>  T val2 = 2 * argc;
>  T valN = T_abs(val1 - val2);
> }
> ----------
> which cause following error:
> GmpTest.cpp:7:5: error: no viable conversion from '__gmp_expr<[...],
> __gmp_unary_expr<__gmp_expr<__mpq_struct[1],
> __gmp_binary_expr<__gmp_expr<mpq_t, mpq_t>, __gmp_expr<mpq_t, mpq_t>,
> __gmp_binary_minus>>, __gmp_unary_minus>>' to '__gmp_expr<[...],
> __gmp_binary_expr<__gmp_expr<mpq_t, mpq_t>, __gmp_expr<mpq_t, mpq_t>,
> __gmp_binary_minus>>'
>  T fVal = -eVal;
>
> Is it a mistake on my side or is there some missing conversion in gmpxx.h?
>
>  Mathieu
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
>

-- 
Marc Glisse


More information about the gmp-discuss mailing list