GCC 16 doesn't optimize away an unused return value from mpz_class post-increment

martin_n_fuller martin_n_fuller at btinternet.com
Thu Feb 19 23:30:12 CET 2026


GCC 16 doesn't optimize away the unused return value from mpz_class 
post-increment in code like "mpz_class a = 1; a++;".
I'm using gcc 16.0.0 on Cygwin. Example code below. Example compilation: 
"g++ -o gmp_test -std=c++26 -O3 -march=native -W -Wall gmp_test.cpp 
-L/usr/local/lib/ -lgmpxx -lgmp"

I am not sure what options there are. I would be very happy with 
[[nodiscard]] as an improvement, or a warning in GMP C++ documentation 
that a++ generates a temporary copy.

Martin

#include <gmpxx.h>

void* (*alloc_func_ptr)(size_t);
size_t alloc_count = 0;

void* allocate_function(size_t alloc_size) {
   alloc_count++;
   return alloc_func_ptr(alloc_size);
}

int main() {
   mp_get_memory_functions(&alloc_func_ptr, NULL, NULL);
   mp_set_memory_functions(allocate_function, NULL, NULL);

   mpz_class a = 1;
   a <<= 1000000;
   alloc_count = 0;
   for (size_t k = 0; k < 1000000; k++) {
     a++;
   }
   printf("Loop allocations %zu\n", alloc_count);
}


More information about the gmp-bugs mailing list