gcc:C23 中的 constexpr 函数?

问题描述 投票:0回答:1

使用 GCC v14.0(应该接近即将发布的 GCC v13.1)玩

constexpr
,我编译了以下模块:

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

constexpr const int x[] = { f(1) };

gcc -std=c2x -c foo.c -O2
但是 GCC 抛出:

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...]

根据 C23 提案中的

constexpr
(pdf) 这应该是正确的语法。该 PDF 没有附带任何示例,所以我在这里缺少什么?

GCC从C++11开始就可以处理

constexpr
,所以在C前端实现它应该是众所周知的成熟技术。

c gcc constexpr c23
1个回答
0
投票

您链接的提案 N2851 未被接受。

只有N3018被纳入C23草案,这个提案不包括

constexpr
关于功能,只有关于对象。

所以有了草稿

constexpr int x[] = { 3 };

constexpr int i = 1;
constexpr int x[] = { i + 2 };
static int y[] = { i + 2 };

是允许的,但不能在初始化器中调用函数或标记函数

constexpr
.

不需要顺便加上

const
constexpr
表示顶级
const
.

© www.soinside.com 2019 - 2024. All rights reserved.