gmpxx.h: __gmp_set_expr(mpf_ptr f, const mpz_class &z) why template?

Antal K antalk at chemres.hu
Tue Feb 28 13:59:56 CET 2006



gmpxx.h at line 3145 says:
---
template <class T>
inline void __gmp_set_expr(mpf_ptr f, const mpz_class &z)
{
  mpf_set_z(f, z.get_mpz_t());
}
---

I do not understand why is it a template, and what T refers to.

A small example: 

-----
#include <gmpxx.h>
mpf_class test_f_of_z( const mpz_class& b ) {
  mpf_class res(b);
  return res;
}
-----

when compiled to assembly:
g++ -S -O3 -I.. test_0001.cc

 grep call test_0001.s
        call    __gmpf_get_default_prec
        call    __gmpf_init2
        call    __gmpz_init_set
        call    __gmpf_set_z
        call    __gmpz_clear
        call    __gmpz_clear
        call    _Unwind_Resume
seems to create a temporary mpz_t.

When the 'template <class T>' line is removed:

grep call test_0001.s
        call    __gmpf_get_default_prec
        call    __gmpf_init2
        call    __gmpf_set_z

the temporary is not created. 

Is there a reason for the 'template <class T>' being there?
Is the temporary mpz_t necessary?

Thanks
  Antal K








More information about the gmp-discuss mailing list