Is this legal C/C++? `int* p = (int[]) {1,2,3} ;`

元气小坏坏 提交于 2019-12-10 03:26:43

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8861266/is-this-legal-c-c-int-p-int-1-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!