1

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.

user17732522
  • 53,019
  • 2
  • 56
  • 105
emacs drives me nuts
  • 2,785
  • 13
  • 23

2 Answers2

4

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.

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Ok, so WG14 messed this one up... What a missed opportunity! – emacs drives me nuts Apr 22 '23 at 14:07
  • @emacsdrivesmenuts It seems they had several implementation concerns and want to collect experience first in with a TS. See TS proposal [N2976](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2976.pdf)) and minutes [N2941](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2941.pdf) for discussion of the N2851 proposal. There are a few issues that the implementation might have in C that are not present in C++, e.g. an array becoming a VLA depending on implementation limits. – user17732522 Apr 22 '23 at 14:23
  • @emacsdrivesmenuts I would also expect that they don't want to effectively require a compiler to be able to fully interpret the language, like current C++ `constexpr` functions require (although N2851 only proposed a subset about equivalent to C++11's `constexpr`). – user17732522 Apr 22 '23 at 14:25
  • 1
    @emacsdrivesmenuts Or rather we should be grateful that they aren't just smoking, tripping and including every single feature that someone proposed... there's still an ambition to keep the language well-defined, useful, portable without thousands of cases of poorly-defined behavior. As a comparison, were we to make a list of all poorly-defined behavior in C++, the summary list would be much longer than the ISO standard itself. That's simply not a desirable state of affairs for a programming language which is to be used in the real world. – Lundin Apr 25 '23 at 07:02
1

Opposite to C++ In C the storage class specifier constexpr may be used only for object definitions.

So this function declaration

constexpr int f (int x)
{
    return x + 2;
}

is incorrect and hence the compiler issues an error.

The constexpr storage class specifier is included in the C23 Standard according to the following document

N3018 constexpr for Object Definitions
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335