Function return values in GMP?

Décio Luiz Gazzoni Filho decio at decpp.net
Mon Feb 27 00:33:32 CET 2006


On Feb 25, 2006, at 9:30 PM, James Wheaton wrote:

> I find it very irritating and redundant that none of
> the GMP functions return GMP-type variables. It makes
> things inflexible so you have to create lots of
> variables, even special variables that you only use
> once. The following code would be ideal, but is
> currently impossible in GMP:
>
> series = mpz_add_ui(series, mpz_ui_pow_ui(i, i));
>
> Is there any reason why the GMP project has chosen to
> make almost all the functions return void?

Perhaps because that implies allocation of temporaries for return  
values, whereas when variables are supplied the user can do his own  
memory management?

Sure, when the variable is used only once, one can avoid explictly  
writing out an mpz_init() (freeing still has to be done, and it's  
easy to forget about that when the allocation is implicit, which is  
arguably a source of errors). However, and especially considering  
that C has no function overloading like C++ does, the GMP designers  
had to choose between one of the formats, and the advantages of 3- 
argument format are clear.

Note that writing a GMP wrapper which does what you want is an  
extremely easy exercise. I could probably write a code generator to  
do this in a couple of hours. Of course I won't do that, but if you  
really wish GMP behaved like that, take your time and write such a  
wrapper.

Also note that this is pure syntactic sugar, and for those looking  
for syntactic sugar, the C++ class interface can't be beaten.  
Personally I only code using that interface.

Décio


More information about the gmp-discuss mailing list