When optimizing, g++ seems to try calling immediate functions inside "if consteval" and then reject what should be valid code. Here is a simple example (which I place in the public domain): ``` // g++ -std=c++23 -c -O2 consteval.cc consteval int plusone(int n) { return n + 1; } constexpr int maybeplusone(int n) { if consteval { return plusone(n); } else { return n; } } int not_consteval() { return maybeplusone(1); } ``` It gives the following error: ``` $ g++ -std=c++23 -c -O2 consteval.cc consteval.cc: In function 'int not_consteval()': consteval.cc:23:22: in 'constexpr' expansion of 'maybeplusone(1)' consteval.cc:13:19: error: call to consteval function 'plusone(n)' is not a constant expression 13 | return plusone(n); | ~~~~~~~^~~ consteval.cc:13:20: error: 'n' is not a constant expression 13 | return plusone(n); | ^ $ ``` The code compiles fine without optimization, and also works with clang 18.
Most likely introduced by the change which was done for https://wg21.link/p2564 (r14-6129-g1f1c432226cf3db399b2a2a627e3c5720b02b1d6 PR 107687 ).
It was introduced by r14-4140: c++: Move consteval folding to cp_fold_r
This might be the same as Bug 115583.
Yes it is a dup. *** This bug has been marked as a duplicate of bug 115583 ***