GMP compile failure

Marc Glisse marc.glisse at
Sat Dec 29 02:00:58 CET 2007

On Fri, 28 Dec 2007, Tim Hockin wrote:

> This (simplified) C++ snippet does not compile.  But it doesn't give
> me the possibly-expected undefined operator error:
>                mpz_class foo(123);
>                mpz_class bar = foo & 0x107;
> /usr/include/gmpxx.h:3413: error: invalid conversion from 'const long
> int' to 'const __mpz_struct*'
> /usr/include/gmpxx.h:3413: error:   initializing argument 3 of 'static
> void __gmp_binary_and::eval(__mpz_struct*, const __mpz_struct*, const
> __mpz_struct*)'

Whereas just "foo & 0x107;" compiles fine.

> I can make it build by explicitly casting the int to mpz_class, but
> that's kind of a pain.  I am converting a codebase to use GMP, and
> this is causing lots of errors.

You can also modify struct __gmp_binary_and in gmpxx.h to (untested):

struct __gmp_binary_and
   static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
   { mpz_and(z, w, v); }
   static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int i)
       mpz_t temp;
       mpz_and(z, w, temp);
   static void eval(mpz_ptr z, unsigned long int i, mpz_srcptr w)
       mpz_t temp;
       mpz_and(z, temp, w);

But if you want to distribute your code, that is not a solution.

> Is there any answer to this, other than "cast it"?

I am not familiar enough with the code to know the right answer.

> why such a cryptic error?

Getting non-cryptic error messages in C++ is... hard. Here the code goes 
through many inline functions to try and avoid any useless copy and the 
type problem is detected only in the middle.

Marc Glisse

More information about the gmp-discuss mailing list