Shorthand for initalising an array of structs

Started by
13 comments, last by NotAYakk 17 years, 7 months ago
Quote:Why arent they POD (plain old data ?) structures ? They are just structs...

If they contain any non-default constructors, operators, or destructors, or contain any types that are not themselves POD types, they are not POD types.

Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.
Advertisement
Did you even read that thread?

Original post from asp_
Yeah Fruny's suggestion generates excellent code for me with no overhead at runtime. If you're really interested I did run some tests. The code I used is below. You can try different variations of it if you're actually interested.

#include <cstdio>class Point3{public:   inline      Point3()      {         mP[0] = 0.0f;         mP[1] = 0.0f;         mP[2] = 0.0f;      };   inline       Point3(const Point3 &p)      {         mP[0] = p.mP[0];         mP[1] = p.mP[1];         mP[2] = p.mP[2];      };   inline      Point3(float x, float y, float z)      {         mP[0] = x;         mP[1] = y;         mP[2] = z;      };   inline void      operator =(const Point3 &p)      {         mP[0] = p.mP[0];         mP[1] = p.mP[1];         mP[2] = p.mP[2];      };   inline void      print()      const      {         printf("%f:%f:%f", mP[0], mP[1], mP[2]);      };private:   float mP[3];};class Plane3b{public:   inline      Plane3b(const Point3 &p0,              const Point3 &p1,              const Point3 &p2)      {         mP[0] = p0;         mP[1] = p1;         mP[2] = p2;      };   void      print()      const      {         printf("P0:");         mP[0].print();         printf(", P1:");         mP[1].print();         printf(", P2:");         mP[2].print();      };private:   Point3 mP[3];};class Plane3a{   public:   inline       Plane3a(const Point3 &p0,               const Point3 &p1,               const Point3 &p2)         : mP0(p0), mP1(p1), mP2(p2)       {      };   inline Point3 &       operator[](size_t idx)          {          return this->*mArr[idx];       };           const Point3&       operator[](size_t idx) const          {          return this->*mArr[idx];       };   void      print()      const      {         printf("P0:");         mP0.print();         printf(", P1:");         mP1.print();         printf(", P2:");         mP2.print();      };private:   static Point3 Plane3a::*mArr[3];   Point3 mP0;   Point3 mP1;   Point3 mP2;};      Point3 Plane3a::*Plane3a::mArr[3] = { &Plane3a::mP0, &Plane3a::mP1, &Plane3a::mP2 };          int main(){   Plane3a p(Point3(1.0f, 2.0f, 3.0f),             Point3(4.0f, 5.0f, 6.0f),             Point3(7.0f, 8.0f, 9.0f));   p.print();}



And here's my summary of the code MSVC 7.1 generates in release build.
////////////////////////////////////////////////////////////////////////////// ARRAY INITIALIZER LIST "HACK", EMPTY MEMBER DEFAULT CONSTRUCTOR (PLANE3A)////////////////////////////////////////////////////////////////////////////// ENTRY POINT00401090 &gt;/$ 83EC 24                    SUB ESP,2400401093  |. 8D4C24 00                  LEA ECX,DWORD PTR SS:[ESP]00401097  |. C74424 00 0000803F         MOV DWORD PTR SS:[ESP],3F8000000040109F  |. C74424 04 00000040         MOV DWORD PTR SS:[ESP+4],40000000004010A7  |. C74424 08 00004040         MOV DWORD PTR SS:[ESP+8],40400000004010AF  |. C74424 0C 00008040         MOV DWORD PTR SS:[ESP+C],40800000004010B7  |. C74424 10 0000A040         MOV DWORD PTR SS:[ESP+10],40A00000004010BF  |. C74424 14 0000C040         MOV DWORD PTR SS:[ESP+14],40C00000004010C7  |. C74424 18 0000E040         MOV DWORD PTR SS:[ESP+18],40E00000004010CF  |. C74424 1C 00000041         MOV DWORD PTR SS:[ESP+1C],41000000004010D7  |. C74424 20 00001041         MOV DWORD PTR SS:[ESP+20],41100000// CALL PLANE3::PRINT()004010DF  |. E8 1CFFFFFF                CALL fruny.00401000004010E4  |. 33C0                       XOR EAX,EAX004010E6  |. 83C4 24                    ADD ESP,24004010E9  \. C3                         RETN////////////////////////////////////////////////////////////////////////////// COPY OPERATOR, EMPTY MEMBER DEFAULT CONSTRUCTOR (PLANE3B)////////////////////////////////////////////////////////////////////////////'// ENTRY POINT00401090 &gt;/$ 83EC 24                    SUB ESP,2400401093  |. 8D4C24 00                  LEA ECX,DWORD PTR SS:[ESP]00401097  |. C74424 00 0000803F         MOV DWORD PTR SS:[ESP],3F8000000040109F  |. C74424 04 00000040         MOV DWORD PTR SS:[ESP+4],40000000004010A7  |. C74424 08 00004040         MOV DWORD PTR SS:[ESP+8],40400000004010AF  |. C74424 0C 00008040         MOV DWORD PTR SS:[ESP+C],40800000004010B7  |. C74424 10 0000A040         MOV DWORD PTR SS:[ESP+10],40A00000004010BF  |. C74424 14 0000C040         MOV DWORD PTR SS:[ESP+14],40C00000004010C7  |. C74424 18 0000E040         MOV DWORD PTR SS:[ESP+18],40E00000004010CF  |. C74424 1C 00000041         MOV DWORD PTR SS:[ESP+1C],41000000004010D7  |. C74424 20 00001041         MOV DWORD PTR SS:[ESP+20],41100000// CALL PLANE3::PRINT() 004010DF  |. E8 1CFFFFFF                CALL fruny.00401000004010E4  |. 33C0                       XOR EAX,EAX004010E6  |. 83C4 24                    ADD ESP,24004010E9  \. C3                         RETN////////////////////////////////////////////////////////////////////////////// COPY OPERATOR, MEMBER DEFAULT CONSTRUCTOR SETS POINT TO 0.0f, 0.0f, 0.0f// (PLANE3B)////////////////////////////////////////////////////////////////////////////// PLANE3::PLANE3()00401090  /$ 8BC1                       MOV EAX,ECX00401092  |. 56                         PUSH ESI00401093  |. 8D70 08                    LEA ESI,DWORD PTR DS:[EAX+8]00401096  |. 57                         PUSH EDI00401097  |. 8BCE                       MOV ECX,ESI00401099  |. BA 03000000                MOV EDX,30040109E  |. 33FF                       XOR EDI,EDI004010A0  |&gt; 8979 F8                    MOV DWORD PTR DS:[ECX-8],EDI004010A3  |. 8979 FC                    MOV DWORD PTR DS:[ECX-4],EDI004010A6  |. 8939                       MOV DWORD PTR DS:[ECX],EDI004010A8  |. 83C1 0C                    ADD ECX,0C004010AB  |. 4A                         DEC EDX004010AC  |.^75 F2                      JNZ SHORT fruny.004010A0004010AE  |. 8B4C24 0C                  MOV ECX,DWORD PTR SS:[ESP+C]004010B2  |. 8B11                       MOV EDX,DWORD PTR DS:[ECX]004010B4  |. 8910                       MOV DWORD PTR DS:[EAX],EDX004010B6  |. 8B51 04                    MOV EDX,DWORD PTR DS:[ECX+4]004010B9  |. 8950 04                    MOV DWORD PTR DS:[EAX+4],EDX004010BC  |. 8B49 08                    MOV ECX,DWORD PTR DS:[ECX+8]004010BF  |. 890E                       MOV DWORD PTR DS:[ESI],ECX004010C1  |. 8B4C24 10                  MOV ECX,DWORD PTR SS:[ESP+10]004010C5  |. 8B11                       MOV EDX,DWORD PTR DS:[ECX]004010C7  |. 8950 0C                    MOV DWORD PTR DS:[EAX+C],EDX004010CA  |. 8B51 04                    MOV EDX,DWORD PTR DS:[ECX+4]004010CD  |. 8950 10                    MOV DWORD PTR DS:[EAX+10],EDX004010D0  |. 8B49 08                    MOV ECX,DWORD PTR DS:[ECX+8]004010D3  |. 8948 14                    MOV DWORD PTR DS:[EAX+14],ECX004010D6  |. 8B4C24 14                  MOV ECX,DWORD PTR SS:[ESP+14]004010DA  |. 8B11                       MOV EDX,DWORD PTR DS:[ECX]004010DC  |. 8950 18                    MOV DWORD PTR DS:[EAX+18],EDX004010DF  |. 8B51 04                    MOV EDX,DWORD PTR DS:[ECX+4]004010E2  |. 8950 1C                    MOV DWORD PTR DS:[EAX+1C],EDX004010E5  |. 8B49 08                    MOV ECX,DWORD PTR DS:[ECX+8]004010E8  |. 5F                         POP EDI004010E9  |. 8948 20                    MOV DWORD PTR DS:[EAX+20],ECX004010EC  |. 5E                         POP ESI004010ED  \. C2 0C00                    RETN 0C// ENTRY POINT004010F0 &gt;/$ 83EC 48                    SUB ESP,48004010F3  |. 8D4424 00                  LEA EAX,DWORD PTR SS:[ESP]004010F7  |. 50                         PUSH EAX004010F8  |. 8D4C24 10                  LEA ECX,DWORD PTR SS:[ESP+10]004010FC  |. 51                         PUSH ECX004010FD  |. 8D5424 20                  LEA EDX,DWORD PTR SS:[ESP+20]00401101  |. 52                         PUSH EDX00401102  |. 8D4C24 30                  LEA ECX,DWORD PTR SS:[ESP+30]00401106  |. C74424 0C 0000E040         MOV DWORD PTR SS:[ESP+C],40E000000040110E  |. C74424 10 00000041         MOV DWORD PTR SS:[ESP+10],4100000000401116  |. C74424 14 00001041         MOV DWORD PTR SS:[ESP+14],411000000040111E  |. C74424 18 00008040         MOV DWORD PTR SS:[ESP+18],4080000000401126  |. C74424 1C 0000A040         MOV DWORD PTR SS:[ESP+1C],40A000000040112E  |. C74424 20 0000C040         MOV DWORD PTR SS:[ESP+20],40C0000000401136  |. C74424 24 0000803F         MOV DWORD PTR SS:[ESP+24],3F8000000040113E  |. C74424 28 00000040         MOV DWORD PTR SS:[ESP+28],4000000000401146  |. C74424 2C 00004040         MOV DWORD PTR SS:[ESP+2C],40400000// PLANE3::PLANE3()0040114E  |. E8 3DFFFFFF                CALL fruny.0040109000401153  |. 8D4C24 24                  LEA ECX,DWORD PTR SS:[ESP+24]// PLANE3::PRINT()00401157  |. E8 A4FEFFFF                CALL fruny.004010000040115C  |. 33C0                       XOR EAX,EAX0040115E  |. 83C4 48                    ADD ESP,4800401161  \. C3                         RETN////////////////////////////////////////////////////////////////////////////// ARRAY INITIALIZER LIST "HACK", MEMBER DEFAULT CONSTRUCTOR SETS POINT TO // 0.0f, 0.0f, 0.0f (PLANE3A)////////////////////////////////////////////////////////////////////////////00401090 &gt;/$ 83EC 24                    SUB ESP,2400401093  |. 8D4C24 00                  LEA ECX,DWORD PTR SS:[ESP]00401097  |. C74424 00 0000803F         MOV DWORD PTR SS:[ESP],3F8000000040109F  |. C74424 04 00000040         MOV DWORD PTR SS:[ESP+4],40000000004010A7  |. C74424 08 00004040         MOV DWORD PTR SS:[ESP+8],40400000004010AF  |. C74424 0C 00008040         MOV DWORD PTR SS:[ESP+C],40800000004010B7  |. C74424 10 0000A040         MOV DWORD PTR SS:[ESP+10],40A00000004010BF  |. C74424 14 0000C040         MOV DWORD PTR SS:[ESP+14],40C00000004010C7  |. C74424 18 0000E040         MOV DWORD PTR SS:[ESP+18],40E00000004010CF  |. C74424 1C 00000041         MOV DWORD PTR SS:[ESP+1C],41000000004010D7  |. C74424 20 00001041         MOV DWORD PTR SS:[ESP+20],41100000004010DF  |. E8 1CFFFFFF                CALL fruny.00401000004010E4  |. 33C0                       XOR EAX,EAX004010E6  |. 83C4 24                    ADD ESP,24004010E9  \. C3                         RETN



Of course this is a pretty stupid test but it shows me all that I need to know. The fact is Fruny's suggestion gives me the ability to address the members as an array and avoid the default constructor.

Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote:Original post by Deyja
Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.


Are you absolutely sure that C++ forbids a "dangling" comma at the end of such a list? At least g++ from GCC 4.1 doesn't have a problem with it, and it is a pretty common thing in other languages.
Quote:Original post by Deyja
Regardless, that's not the error. You've got extra commas. Fix the syntax error before you look for other possibilities.


No, that's not it.

The error was that he tried to initialise the array seperately from the declaration.


int[3] array = {1,2,3};//OK



int[3] array;
array = {1,2,3};//Error

"D3DCOLOR_XRGB( 255, 255, 255)" lead me to believe that they wheren't POD structures.

On second thought, D3DCOLOR_XRGB is a macro, not a constructor for an object of type D3DCOLOR_XRGB. :)

So yes, they are POD structures. Ik. :)

This topic is closed to new replies.

Advertisement