C++ factorial, primorial

Tor Myklebust tmyklebu at csclub.uwaterloo.ca
Sun Jun 29 16:40:52 UTC 2014


On Sun, 29 Jun 2014, Marc Glisse wrote:

> 1) factorial(42)
> 2) factorial(z)
> 3) mpz_class::factorial(42)
> 4) mpz_class::factorial(z)
> 5) gmpxx::factorial(42)
> 6) gmpxx::factorial(z)
> 7) z.factorial()
>
> 1 is bad, as said above. 7 is not so good because most of the time we want 
> to compute factorial of an int and don't want to convert it to mpz_class 
> first. All the others seem ok to me. Supporting both 3-4 and 5-6 seems 
> unnecessary. 5-6 have the advantage that a user can write:
> using gmpxx::factorial;
> and then use 1 and 2.

There doesn't seem to be a namespace gmpxx in existing gmp, so 5 and 6 
seem a little weird.  (If there were, and mpz_class were in namespace 
gmpxx, Koenig lookup would make 5 and 6 unequivocally the right thing to 
do.)

Now...mpz_class is a typedef for __gmp_expr<mpz_t, mpz_t>.  __gmp_expr is 
an implementation detail that client code shouldn't touch.  What about 
tossing __gmp_expr into namespace gmpxx, leaving mpz_class as a typedef in 
the top-level namespace?  Maybe you break the ABI doing this (or maybe 
not), but you can move everything into a namespace where you can freely 
define free functions without worrying about pollution.


More information about the gmp-discuss mailing list