这是合法的 C/C++ 吗? `int* p = (int[]) {1,2,3} ;`

发布于 2024-12-27 03:29:31 字数 297 浏览 2 评论 0原文

我的这个答案生成了一些评论,声称以下构造不是合法的 C/C++:(

void f (int* a) ;
f ((int[]){1,2,3,4,0}) ;

请参阅 此 ideone 链接 以获得完整程序)。但我们没能解决这个问题。有人能解释一下吗?各种标准都有什么规定?

This answer of mine generated some comments claiming that the following construct is not legal C/C++:

void f (int* a) ;
f ((int[]){1,2,3,4,0}) ;

(see this ideone link for a full program). But we weren't able to resolve the issue. Can anybody shed any light on this? What do the various standards have to say?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

树深时见影 2025-01-03 03:29:31

据我所知,它是有效的 C99 - 这是传递一个复合文字。

C99 标准以此为例(§6.5.2.5/9):

示例 1 文件范围定义

int *p = (int []){2, 4};

将 p 初始化为指向两个 int 数组的第一个元素,第一个值为 2,第二个值为 4。该复合文字中的表达式必须是常量。未命名对象具有静态存储持续时间。

请注意,这里的 (int []) 并不是强制转换。

但这不是有效的 C++ 构造,复合文字不是 C++ 标准的一部分(包括 C++11)。一些编译器允许它作为扩展。 (GCC 确实如此,通过 -Wall -pedantic 来获取有关它的诊断信息。IBM xlC 允许将其作为扩展也是。)

It's valid C99 as far as I can tell - that's passing a compound literal.

The C99 standard has this as an example (§6.5.2.5/9):

EXAMPLE 1 The file scope definition

int *p = (int []){2, 4};

initializes p to point to the first element of an array of two ints, the first having the value two and the second, four. The expressions in this compound literal are required to be constant. The unnamed object has static storage duration.

Note that the (int []) thing is not a cast here.

This is not a valid C++ construct though, compound literals are not part of the C++ standard (C++11 included). Some compilers allow it as an extension. (GCC does, pass -Wall -pedantic to get a diagnostics about it. IBM xlC allows it as an extension too.)

街角卖回忆 2025-01-03 03:29:31

作为参数传递给函数的表达式是复合文字的示例。这些在 C99 中是合法的,但在 C++98 中不合法。

例如,请参见 N897“C99 标准的基本原理草案。” 另请参阅 GCC 文档的此部分

The expression passed as argument to the function is an example of a compound literal. These are legal in C99, but not in C++98.

See for example section 6.4.4 "Constants" and 6.8 "Statements and blocks" in N897 "A draft rationale for the C99 standard." See also this section of GCC documentation.

洋洋洒洒 2025-01-03 03:29:31

嗯,我认为根据 C++11 它是有效的。第 5.2 节:

postfix-expression:
    ...
    typename-specifier ( expression-listopt )
    simple-type-specifier braced-init-list
    typename-specifier braced-init-list
    ...
expression-list:
    initializer-list

编辑:
经过更多阅读后,我得出结论,它实际上是无效,因为你不能使用这样的后缀表达式。应该有一些主要的表达。

Well, I think it is valid according to C++11. Section 5.2:

postfix-expression:
    ...
    typename-specifier ( expression-listopt )
    simple-type-specifier braced-init-list
    typename-specifier braced-init-list
    ...
expression-list:
    initializer-list

EDIT:
After some more reading I came to conclusion it's actually invalid, because you cannot use postfix expression like that. There should be some primary expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文