ema*_*uts 2 c gcc constexpr c23
在使用constexprGCC v14.0(应该接近即将发布的 GCC v13.1)时,我编译了以下模块:
constexpr int f (int x)
{
return x + 2;
}
constexpr const int x[] = { f(1) };
Run Code Online (Sandbox Code Playgroud)
但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...]
Run Code Online (Sandbox Code Playgroud)
根据constexprC23 提案 (pdf),这应该是正确的语法。不过该 PDF 没有附带任何示例,那么我在这里缺少什么?
GCC 自 C++11 起就可以处理constexpr,因此在 C 前端实现它应该是已知且成熟的技术。
您链接的提案N2851未被接受。
只有N3018被纳入C23草案中,并且该提案不包括constexpr功能,仅包括对象。
所以有了草案
constexpr int x[] = { 3 };
Run Code Online (Sandbox Code Playgroud)
和
constexpr int i = 1;
constexpr int x[] = { i + 2 };
static int y[] = { i + 2 };
Run Code Online (Sandbox Code Playgroud)
是允许的,但不能在初始化程序中调用函数或标记函数constexpr。
const顺便说一句,你不需要添加。constexpr意味着顶级const。
| 归档时间: |
|
| 查看次数: |
1380 次 |
| 最近记录: |