开发者

is this invalid C++?

struct HybridExpression {
    RawExpressionElement *ree;
    ExpressionNode *en;
};    

vector<HybridExpression> hexpression;

hexpression.insert(hexpression.begin() + starti, 
        (HybridExpression) {NULL, en}); 

gcc builds without warning but visual studio 2010 wont even comp开发者_开发知识库ile it.

It doesnt like this bit: (HybridExpression) {NULL, en}


This is using a part of the C programming language that is not included in C++, it's called "compound literal". g++ -ansi will diagnose this, saying

warning: ISO C++ forbids compound-literals

This is not a part of C++0x.

The C++0x compatible syntax would have been

hexpression.insert(hexpression.begin() + starti, HybridExpression{NULL, en});

To quote the C99 standard, paragraph 6.5.2.5:

A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.


According to http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/C_002b_002b-Extensions.html#C_002b_002b-Extensions, you can use C extensions (including C99 stuff) in C++ programs compiled with GCC.

Compound literals is the extension you're actually using.


This is not valid C++ in the current Standard. I think GCC allows it because of a GCC-specific compiler extension.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜