Playing around with constexpr
with GCC v14.0 (which should be close to the soon to be released GCC v13.1), I compiled the following module:
constexpr int f (int x)
{
return x + 2;
}
constexpr const int x[] = { f(1) };
with gcc -std=c2x -c foo.c -O2
but GCC throws:
foo.c:1:1: error: 'constexpr' requires an initialized data declaration
1 | constexpr int f (int x)
| ^~~~~~~~~
[...f'up errors due to the one above...]
According to constexpr
in C23 proposal (pdf) this should be correct syntax. That PDF doesn't come with any examples though, so what I am missing here?
GCC can deal with constexpr
since C++11, so implementing it in the C frontend should be known and mature technology.
The proposal N2851 that you linked was not accepted.
Only N3018 has been incorporated into the C23 draft and this proposal does not include constexpr
on functions, only on objects.
So with the draft
constexpr int x[] = { 3 };
and
constexpr int i = 1;
constexpr int x[] = { i + 2 };
static int y[] = { i + 2 };
are allowed, but you can't call a function in the initializer or mark a function constexpr
.
You don't need to add const
by the way. constexpr
implies a top-level const
.