![]() |
|
const struct initialization: why does this (not) work? |
amarillion
Member #940
January 2001
![]() |
Lately I've settled on eclipse CDT as my primary IDE for C++ development. It has it's own built-in incremental C++ compiler which doesn't always exactly agree with GCC. I came across this snippet while brushing off some old code: 1class LevelData
2{
3 public:
4 int map_sizex;
5 int map_sizey;
6
7 int targets;
8 int enemy_tanks;
9 int enemy_turrets;
10 int enemy_sam;
11 int enemy_bunker;
12 int buildings;
13 bool haschopper;
14 bool hastank;
15};
16
17const int LEVEL_NUM = 6;
18
19// "Syntax Error" ON THE NEXT LINE
20const LevelData levels[LEVEL_NUM] =
21{
22 { // level 1
23 map_sizex : 2048,
24 map_sizey : 2048,
25 targets : 1,
26 enemy_tanks : 1,
27 enemy_turrets : 20,
28 enemy_sam : 0,
29 enemy_bunker : 20,
30 buildings : 40,
31 haschopper : false,
32 hastank : true,
33 },
34 { // level 2
35 map_sizex : 2048,
36 map_sizey : 2048,
37 targets : 1,
38 enemy_tanks : 2,
39 enemy_turrets : 20,
40 enemy_sam : 0, // no sam, because you don't have tank to help
41 enemy_bunker : 10,
42 buildings : 40,
43 haschopper : true,
44 hastank : false,
45 },
46// 4 more omitted
47}
GCC compiles this just fine. However, the eclipse compiler signals a "syntax error" on the const array initializer (marked with a comment). Looking at this code, I don't know what I was thinking. Why is this way of initializing structs even compiling at all in GCC? Is this a non-standard extension of the language? -- |
Neil Walker
Member #210
April 2000
![]() |
Eclipse does not have it's own compiler, it uses GCC/mingw. But I've never seen that syntax for initialising arrays of objects before, is it a gcc thing? Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Stas B.
Member #9,615
March 2008
|
It's probably a non-standard extension like you said. The standard way to do the same in C99 looks like this: struct Blah { int a, b, c; }; const struct Blah blah = { .a = 10, .b = 20, .c = 30, };
I don't think that works in C++ though. |
amarillion
Member #940
January 2001
![]() |
Neil Walker said: Eclipse does not have it's own compiler, it uses GCC/mingw. That is not correct. Eclipse CDT does have its own internal compiler, which is used for indexing, code completion and compilation checking while you type. It uses GCC/MinGW for the final build. -- |
Neil Walker
Member #210
April 2000
![]() |
I didn't know that. I just gave up on CTD after I realised it wanted to force the Java folder/file hierarchy and project management system on me and it simply didn't fit with C++ so I moved to NetBeans. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Peter Wang
Member #23
April 2000
|
Obsolete gcc extension: http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html ("Another syntax which has the same meaning...")
|
Neil Walker
Member #210
April 2000
![]() |
Why create extensions, just stick with the fecking rules, they're there for a reason. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Audric
Member #907
January 2001
|
The C language doesn't accept single-line comments // (until C99) or embedded assembler asm {} (never). Good compilers implemented useful features before waiting for all vendors to agree on them. |
Stas B.
Member #9,615
March 2008
|
Neil Walker said: Why create extensions, just stick with the fecking rules, they're there for a reason.
Because why not? |
Neil Walker
Member #210
April 2000
![]() |
aye, but // is a de-facto standard and fairly harmless, modifying the way things are created/initialised/tested is not. That's my view anyway. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Peter Wang
Member #23
April 2000
|
Someone felt the need for something, compiler authors implement it as an extension and see if it works. If enough people (or the right people) think the feature is worthwhile then it gets codified into a standard, using the lessons learnt from the extension.
|
amarillion
Member #940
January 2001
![]() |
Neil Walker said: I just gave up on CTD after I realised it wanted to force the Java folder/file hierarchy and project management system on me and it simply didn't fit with C++ so I moved to NetBeans. When I first started using eclipse, I really had to get used to the idea of multiple projects in a workspace. But now I love the fact that I can browse and search all my code across multiple projects. Also, I figured out later that it's not required to have your code live under ~/eclipse/workspace or whatever, you can always override the defaults for project locations. I don't know what other issues could arise from the Java project structure. Maybe you don't like separating header and source files? But I have always done that, so that doesn't bug me. -- |
Neil Walker
Member #210
April 2000
![]() |
I think it was I wanted to include the source code from other projects in the build but keep them in a different folder. CTD either wanted me to simply link to the library files or have the code as a completely new eclipse project, neither of which is what I wanted to do. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
amarillion
Member #940
January 2001
![]() |
Mabye this is something new, but you can definitely do that. I do that in my projects too, I have a "shared" folder that is shared between a lot of projects. Simply go to Project properties -> C/C++ general -> Paths and Symbols -> Source Location, and click "Link Folder". You can link to any folder on your system. -- |
|