is this invalid C++? [duplicate]
Asked Answered
P

3

12
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 compile it.

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

Plainspoken answered 1/4, 2011 at 15:53 Comment(1)
What's the compiler error message?Gowon
M
20

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.

Methodical answered 1/4, 2011 at 15:56 Comment(6)
@tm1rbrt: you can still rage at them for abandoning C!Methodical
@Cubbi: Did you install Microsoft Visual C? No, it's Microsoft Visual C++. They haven't made any pretense at support for the C standard since Microsoft C/C++ Compiler 7.0, which I think was release before C99 was finished.Linin
@Cubbi, didn't you mean HybridExpression(NULL, en)? Note parenthesis, not curly brace.Ephram
@Rob Adams: Round parentheses would have called HybridExpression's two-argument constructor. Curly braces initialize it using the initializer list: www2.research.att.com/~bs/C++0xFAQ.html#init-listMethodical
Because of uniform initialization, it will also work without explicitly stating the HybridExpression object: hexpression.insert(hexpression.begin() + starti, {NULL, en});Reneerenegade
The error shows up under MacPort's GCC 5 with -std=c++11, too.Lingerie
R
3

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.

Romelda answered 1/4, 2011 at 15:58 Comment(4)
compound literals are not included in C++0x, at least as of N3242, and non-constant initializers are not the extension used in this case (they are gcc's extension to the C89 language, not to C++).Methodical
@Cubbi: as the 1st link states, you can use gcc extensions to the C language for C++.Romelda
yes, the first link is good, that's why I didn't downvote. The second one is not relevant. It should have been gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/…Methodical
@Cubbi: Thanks, I did not find it the first time.Romelda
B
0

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

Branham answered 1/4, 2011 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.