Here are more about strange behaviour of GMP C++ Class Interface, related to the C++ auto
keyword. My previous question was also about this keyword, and its removal solved my problem. However it was compilation problem, but now it’s runtime one.
The simple test below compiles OK but coredumps.
#include <iostream>
#include <gmpxx.h>
auto test()
{
return mpz_class(2) * 2;
}
int main()
{
std::cout << test() << std::endl;
}
If to replace the keyword auto
by the mpz_class
, then the test will run correctly. Also – if to omit the compilation flag -O3
, then the test will run, but will print a wrong number, which will vary with each execution (which usually happens when the execution depends on random uninitialised data).
It looks like the gmpxx.h
header contains something, which makes the C++ compiler to generate wrong code in this case. How to neutralize this something?
A possible answer would be “Don’t use the auto
return type”, however I prefer to use it – that simplifies modifications.
The system environment:
- OS: Ubuntu 22.04.4 LTS
- Compiler: g++ (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
- Compiler/linker flags: -O3 -Wall -std=c++20 -lgmpxx -lgmp
- GMP version: 6.2.1