unions

What's the proper format for a union initializer list?

孤者浪人 提交于 2019-12-22 15:54:51
问题 Say I have a class/struct with a single union in it... something like: struct Box { union { AS128 intr; struct { AS32 a, b, c, d; }; }; }; What's the proper way to do an initializer list for this kind of data type? Box iMyBoxA = { 10, 10, 100, 100 }; Box iMyBoxB = ( Box ){ 10, 10, 100, 100 }; The 'A' option above works in many cases, but isn't completely portable... giving the error "no known conversion for argument 1 from 'brace-enclosed initializer list'". And the second doesn't compile

When a union object is copied, is a member subobject created?

瘦欲@ 提交于 2019-12-22 08:40:08
问题 When another member of a union is accessed, the C++ standard used to be silent on what happens, but that was fixed to explain that member access to a union object was allowed for the purpose of assigning to that yet no existent object, which would magically create the object, by assignment to it or one of its members. Essentially the member access operator returns a promise of a future object and you have to use it with an assignment. Given union U { int i; long l; }; We can create a i or l

Can we access a member of a non-existing union?

别来无恙 提交于 2019-12-22 06:45:30
问题 In the c++ standard, in [basic.lval]/11.6 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:[...] an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),[...] This sentence is part of the strict-aliasing rule. Can it allow us to

C overcoming aliasing restrictions (unions?)

☆樱花仙子☆ 提交于 2019-12-22 05:04:24
问题 Assume I have a sample source file, test.c, which I am compiling like so: $ gcc -03 -Wall test.c looks something like this .. /// CMP128(x, y) // // arguments // x - any pointer to an 128-bit int // y - any pointer to an 128-bit int // // returns -1, 0, or 1 if x is less than, equal to, or greater than y // #define CMP128(x, y) // magic goes here // example usages uint8_t A[16]; uint16_t B[8]; uint32_t C[4]; uint64_t D[2]; struct in6_addr E; uint8_t* F; // use CMP128 on any combination of

How to expose C++ unions to Lua

纵饮孤独 提交于 2019-12-22 04:38:14
问题 For a project I'm working on, I need to expose some C++ classes in another library to Lua. Unfortunately, one of the most important classes in this library has lots of Unions and Enums (the sf::Event class from SFML), and from a quick Google search I've discovered there is nothing on exposing C++ Unions to Lua. I don't mind if it's being exposed with the Lua/C API, with a library or with a binding generator, as long as it works. But, I'd prefer not to use a binding generator, because I want

What's the use of const union members? Aren't they quite pointless?

邮差的信 提交于 2019-12-22 04:06:35
问题 In the comments to this answer, Koushik raised a very valid point. Take the following: union U { int x; const T y; }; (I choose T such that there is no common initial sequence of layout compatibility here, meaning only one member may be active at any given time per [C++11: 9.5/1] .) Since only one member may be "active" at any one time (made active by writing to it), and y cannot be written to after initialisation, isn't this rather pointless? I mean, y can only be read from until the first

C++ anonymous structs

巧了我就是萌 提交于 2019-12-22 04:03:06
问题 I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int bit_5: 1; unsigned int bit_6: 1; unsigned int bit_7: 1; }; struct { unsigned int nibble_0: 4; unsigned int nibble_1: 4; }; unsigned char byte; }; It works nice, but it also generates this warning: warning: ISO C++ prohibits anonymous structs [-pedantic] Ok, nice to know. But.

Strange C++ boolean casting behaviour (true!=true)

ぃ、小莉子 提交于 2019-12-22 03:50:34
问题 Just read on an internal university thread: #include <iostream> using namespace std; union zt { bool b; int i; }; int main() { zt w; bool a,b; a=1; b=2; cerr<<(bool)2<<static_cast<bool>(2)<<endl; //11 cerr<<a<<b<<(a==b)<<endl; //111 w.i=2; int q=w.b; cerr<<(bool)q<<q<<w.b<<((bool)((int)w.b))<<w.i<<(w.b==a)<<endl; //122220 cerr<<((w.b==a)?'T':'F')<<endl; //F } So a , b and w.b are all declared as bool . a is assigned 1 , b is assigned 2 , and the internal representation of w.b is changed to 2

Use of Union with reference

喜你入骨 提交于 2019-12-21 12:19:03
问题 At work I've been using linux and the GCC compiler for C++11 and C++14. In some of the code at work, I've used a union to store both a reference and a pointer, as so: (Simplified to just the important parts) struct MyStruct { //Stuff union { double& x; double* x_ptr; }; MyStruct(double& value) : x(value) {} //More stuff }; I believe this code is clear, readable, unambiguous, and provides a convenient way to store references which can be shifted to something else. It provides easily

union initialisation

ぃ、小莉子 提交于 2019-12-21 12:13:07
问题 I'm attempting to globally initialise a union as in the following example: #include <cstdio> typedef union { char t[4]; int i; } a; enum { w = 5000, x, y, z }; a temp = {w}; int main() { printf("%d %d %d %d %d\n", temp.t[0],temp.t[1],temp.t[2],temp.t[3],temp.i); return 0; } However, if you run the code, you'll note that neither of temp.i or temp.t[...] actually give the correct item i initialised the union with. I'd imagine this would be avoided if I could manually initialise the integer