GCC 16 doesn't optimize away an unused return value from mpz_class post-increment
marco.bodrato at tutanota.com
marco.bodrato at tutanota.com
Sat Feb 21 08:40:10 CET 2026
Ciao,
20 feb 2026, 23:15 da marc.glisse at inria.fr:
> On Thu, 19 Feb 2026, martin_n_fuller wrote:
>
>> GCC 16 doesn't optimize away the unused return value from mpz_class post-increment in code like "mpz_class a = 1; a++;".
>>
>
> That's just normal C++ rules, nothing specific to GMP or gcc-16. There are very few cases where optimizations are allowed to change the output of a program (like the one you included in your email), and this is not one of them. And a++ both increments and returns the old value, so it has to create a separate object.
>
When I learned this, I started replacing every "a++" with "++a" wherever they are equivalent (i.e. when the return value is not used). And of course for the same reason I prefer "--i" to "i--".
I mean, I should have written the loop like:
for (size_t k = 0; k < 1000000; ++k) {
++a;
}
Should we add in the documentation a note: "Remember that ++C is more efficient that C++"? :-D
Ĝis,
mb
More information about the gmp-bugs
mailing list