It's not valid ISO C++...
O_oAlso, why would you want to bother with code that breaks so easily by simply changing compilers/platforms or even adding or reordering members?
Why did you write similar code to benefit from the C++ mechanisms while also benefiting from SSE when available?
Actually, that code has a practical purpose. Let us look at a purely cosmetic situation; why do you write code having member variables of the form `_E???' and `_M???'?
I'm not even picking on you; I just find question rather bizarre from someone who also writes code in clear violation of the standard for no better reason than the look of the thing.
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
o_OIt's not valid ISO C++...
Wow.
Soma
“Salem Was Wrong!” -- Pedant Necromancer
“Four isn't random!” -- Gibbering Mouther
Dunno what you're referring to here...
Just because I do X doesn't mean I think it's a good idea that others do X.Actually, that code has a practical purpose. Let us look at a purely cosmetic situation; why do you write code having member variables of the form `_E???' and `_M???'?
I'm not even picking on you; I just find question rather bizarre from someone who also writes code in clear violation of the standard for no better reason than the look of the thing.
Speaking of structs, I saw this example in a book a few weeks ago. I couldn't remember what it's called when you do this.
You have a struct that is declared like this:
When you need to store 256 colors in the color table, you allocate like this (it's in C)Code:typedef struct tagBITMAPCOREINFO { BITMAPCOREHEADER bmciHeader; // Core-header structure RGBTRIPLE bmciColors[1]; // Color table } BITMAPCOREINFO, *PBITMAPCOREINFO;
Then you can access 256 indexes of the color table.Code:PBITMAPCOREINFO pbmci = malloc( sizeof( BITMAPCOREINFO ) + 255 * sizeof( RGBTRIPLE ) );
This seemed a bit weird to me. Aren't I actually accessing outside of the bounds of the array, and into the extra memory allocated for the structure? Is this ok (and does this concept have a name)?Code:pbmci->bmciColors[200] = { 0xC0, 0xC0, 0xC0 };
WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;
It is a hack to simulate a flexible array member. I believe it is known as the "struct hack".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
To remove that pedantic error, you need to modify that internal struct:
Now you can assign to them (x, y, z) by the point identifier.Code:struct point_t { union { struct { float x, y, z; } point; float p[3]; }; };
Just to be clear, so I can understand this as well:Code:point_t pt; pt.point.x = 1.1; pt.point.y = 1.2; pt.point.z = 1.3;
Is this "acceptable"?Code:#include <iostream> struct point_t { point_t() : point_t(0, 0, 0) {} point_t(float x, float y, float z) { static_assert(sizeof(point) == sizeof(float) * 3, "structure has padding"); point.x = x; point.y = y; point.z = z; } union { struct { float x, y, z; } point; float p[3]; }; }; int main() { using namespace std; point_t pt(1.3, 1.2, 5.7); cout << pt.point.x << ", " << pt.point.y << ", " << pt.point.z << endl; cout << pt.p[0] << ", " << pt.p[1] << ", " << pt.p[2] << endl; }
I haven't personally tested it but thank you for fixing the pedantic error!