Problem (with fix) concerning GMP's C++ random number class

If you take the following code, that comes straight from the manual of GMP 4.1, and try to compile it with a strictly conforming C++ compiler, you will see that it does not compile.
--------------------------------------------- #include <gmpxx.h>
void foo() { gmp_randclass rg(gmp_randinit_default); } ---------------------------------------------
The reason is that, while the constructors for `gmp_randclass' have all been declared to take a C++ function pointer, the invocation above passes a C function pointer. This is not allowed by the C++ standard. One possibility to rectify things is the patch below, which changes the declarations of the constructors. Of course, the dual change is also possible, the point being that C linkage and C++ linkage are incompatible. All the best
Roberto

Roberto Bagnara bagnara@cs.unipr.it writes:
a strictly conforming C++ compiler,
For instance? g++ 3.2 -pedantic has no objections to the current code.
- extern "C" typedef void GMP_RANDINIT_DEFAULT(gmp_randstate_t);
Can this be private to gmp_randclass, or given within the definitions of the constructors? If not then they'll want to be prefixed like __gmp_randinit_default_t, or some such.

Kevin Ryde wrote:
Roberto Bagnara bagnara@cs.unipr.it writes:
a strictly conforming C++ compiler,
For instance? g++ 3.2 -pedantic has no objections to the current code.
For instance Comeau C++ compiler with option --strict and (IIRC, but I don't have it handy at the moment) Intel's icc compiler with the -Xc option (select strict ANSI C/C++ conformance dialect). However, the point is that the code I have indicated violates the standard; as C++ compiler are moving more and more towards standard conformance the problem may show up at any time with any compiler.
- extern "C" typedef void GMP_RANDINIT_DEFAULT(gmp_randstate_t);
Can this be private to gmp_randclass, or given within the definitions of the constructors? If not then they'll want to be prefixed like __gmp_randinit_default_t, or some such.
You are right of course (indeed I was expecting Gerardo, as the father, of the C++ interface, would elaborate on the idea so as to suit his taste). I will provide a better patch at the soonest (since I really hope it could be incorporated in the next GMP's release). All the best
Roberto

Kevin Ryde wrote:
- extern "C" typedef void GMP_RANDINIT_DEFAULT(gmp_randstate_t);
Can this be private to gmp_randclass, or given within the definitions of the constructors?
No, unfortunately it cannot.
If not then they'll want to be prefixed like __gmp_randinit_default_t, or some such.
The patch below implements this idea. Tested with G++ 3.2, Comeau C/C++ 4.3.0.1 (both with and without --strict) and Intel(R) C++ Compiler for 32-bit applications, Version 6.0.1, Build 20020822Z (the latest version, both with and without -Xc). All the best
Roberto
participants (2)
-
Kevin Ryde
-
Roberto Bagnara