Tested on a somewhat recent trunk build as well as 4.7-some branch version. // Preliminaries. extern int nonconst_func(int); constexpr int identity(int x) { return x; } constexpr int zero() { return identity(0); } constexpr int one() { return identity(1); } // Correctly accepted. constexpr int three = one() ? 3 : nonconst_func(0); // Incorrectly accepted. See [dcl.constexpr] #5: // For a constexpr function, if no function argument values exist // such that the function invocation sub-stitution would produce a // constant expression (5.19), the program is ill-formed; no diagnostic // required. constexpr int bogus() { return zero () ? 3 : nonconst_func(0); } // Correctly rejected (not sure why). constexpr int correct_error() { return nonconst_func(0); } // Correctly rejected. constexpr int z = bogus(); // This is also correctly rejected. constexpr int correct_failure() { return 0 ? 3 : nonconst_func(0); }
... the program is ill-formed; no diagnostic required. ^^^^^^^^^^^^^^^^^^^^^^^ GCC doesn't immediately see that the first operand of ?: is zero or non-zero in that function, so assumes it could be either zero or non-zero and doesn't diagnose, you get diagnostics only when you are actually using the function in some constexpr var initializer or other context where a constant expression is required.
I clearly failed at reading comprehension yesterday. Maybe this should be considered as more of an enhancement request (like PR54021): it would be nicer for the user if constexpr worked the same with an without optimization. Otherwise there'll probably be reports of code that builds at -O2 but not -O0.
Let's add Jason in CC (about the -O0 vs -O2 thing, in particular)
This testcase behaves the same at -O0 or -O2. Seems like a simple enough enhancement, though.
Author: jason Date: Wed Jul 25 14:56:57 2012 New Revision: 189851 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189851 Log: PR c++/54020 * semantics.c (potential_constant_expression_1) [COND_EXPR]: Call maybe_constant_value. Added: trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-neg2.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/semantics.c
Fixed for 4.8.