Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ GNU designated structure initialization not recognized in Eclipse

The CDT parser reports a syntax error for the structure initialization:

typedef struct MyStruct
{
    int a;
    float b;
};

int main( void )
{
    // GNU C extension format
    MyStruct s = {a : 1, b : 2};
    // C99 standard format
//    MyStruct s = {.a = 1, .b = 2};

    return 0;
}

While GCC lists the : form as obsolete, it would seem that it has not been deprecated nor removed. In C99 I would certainly use the standard .<name> = form but for C++, the : is the only option that I am aware of for designated initialization.

I have tried setting my toolchain to both MinGW and Cross GCC, but neither seem to work.

How can I get Eclipse to recognize this syntax? It's not a big deal for one line but it carries through to every other instance of the variable since Eclipse does not realize it is declared.

like image 489
altendky Avatar asked Jan 14 '13 19:01

altendky


1 Answers

The . form is only available in C99 and not in any flavor of C++. In C++ your only standards-compliant options are ordered initialization or constructors.

You can use chaining with appropriate reference returning methods to create a similar interface (here a and b are methods rather than variables):

MyStruct s;
s.a(1).b(2);
like image 94
Mark B Avatar answered Sep 17 '22 12:09

Mark B