https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122424
Bug ID: 122424
Summary: constexpr evaluation rejects unevaluated call to
non-constexpr function in array initializer
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bence.kodaj at gmail dot com
Target Milestone: ---
Reproducer:
int non_constexpr() { return 1970; }
constexpr int f() {
int not_array = __builtin_is_constant_evaluated()
? 2
: non_constexpr();
int array[] = { __builtin_is_constant_evaluated()
? 16
: non_constexpr()
};
return not_array + array[0];
}
constexpr int v = f();
In function 'constexpr int f()':
<source>:9:9: warning: call to non-'constexpr' function 'int non_constexpr()'
[-Winvalid-constexpr]
8 | int array[] = { __builtin_is_constant_evaluated()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 | ? 16
| ^~~~
10 | : non_constexpr()
| ~~~~~~~~~~~~~~~~~
<source>:1:5: note: 'int non_constexpr()' declared here
1 | int non_constexpr() { return 1970; }
| ^~~~~~~~~~~~~
<source>: At global scope:
<source>:16:20: error: 'constexpr int f()' called in a constant expression
16 | constexpr int v = f();
| ~^~
<source>:3:15: note: 'constexpr int f()' declared here
3 | constexpr int f() {
| ^
-------
Notes:
- This is somewhat similar to bug 86678 in that when f() is evaluated in a
constant context, the calls to non_constexpr() are not evaluated at all.
- GCC versions 9.1 to 12.5 accept the code.
- Versions 13.1 to 15.2 reject it.
- Notice that they only reject the definition of `array`; they accept that of
`not_array`.
- Latest Clang and MSVC accept the code.