Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining static const structs

Tags:

c++

c

symbian

This question is related to Symbian OS yet I think that C/C++ veteran can help me too. I'm compiling an open source library to Symbian OS. Using a GCCE compiler it compiles with no errors (after some tinkering :) ). I changed compiler to ARMV5 and now I have multiple errors with the definitions of static const structs, for example: I have a struct:

typedef struct Foos{
    int a;
    int b;
} Foos;

And the following definition of a const struct of type Foos

static const Foos foo = {
    .a = 1,
    .b = 2,
};

GCCE has no problem with this one and ARMV5 goes "expected an expression" error on the ".a = 1, .b = 2,". From what I googled regarding this I reckon that this method should be legal in C but illegal in C++, if that's the case then what are the possibilities for declaring const structs in C++ ? If that's not the case then any other help will be appreciated.

Thanks in advance :)

like image 341
dudico Avatar asked Mar 29 '09 14:03

dudico


People also ask

What is a static const struct?

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.

Can we use const and static together?

So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.

Can struct members be const?

You can const individual members of a struct. Declaring an entire instance of a struct with a const qualifier is the same as creating a special-purpose copy of the struct with all members specified as const .

Can you make a struct static?

A structure declaration cannot be declared static, but its instancies can. You cannot have static members inside a structure because the members of a structure inherist the storage class of the containing struct. So if a structure is declared to be static all members are static even included substructures.


2 Answers

static const struct Foos foo = { 1, 2 };

Compiles with both g++ and gcc.

You could ofcourse, as onebyone points out, define a constructor:

typedef struct Foos {
    int a;
    int b;
    Foos(int a, int b) : a(a), b(b) {}
};

Which you would initalize like so:

static const struct Foos foo(1, 2);
like image 62
Daniel Sloof Avatar answered Sep 20 '22 20:09

Daniel Sloof


That's legal C99, but not legal C89 or C++. Presumably you're compiling this as C++, so if you use compiler options to enforce standards-compliance, then GCCE will reject it too.

You can do foo = {1, 2}; in C or C++. Obviously you lose the benefit of the field names being right there: you have to rely on getting the order right.

Another good option in C++ is to define a constructor for your struct, and initialize with static const Foos foo(1,2);. This does prevent the struct being POD, however, so you can't make the same assumptions about its representation in memory.

like image 33
Steve Jessop Avatar answered Sep 20 '22 20:09

Steve Jessop