unions

C++11 anonymous union with non-trivial members

我只是一个虾纸丫 提交于 2019-11-26 13:03:22
问题 I\'m updating a struct of mine and I was wanting to add a std::string member to it. The original struct looks like this: struct Value { uint64_t lastUpdated; union { uint64_t ui; int64_t i; float f; bool b; }; }; Just adding a std::string member to the union, of course, causes a compile error, because one would normally need to add the non-trivial constructors of the object. In the case of std::string (text from informit.com) Since std::string defines all of the six special member functions,

union 'punning' structs w/ “common initial sequence”: Why does C (99+), but not C++, stipulate a 'visible declaration of the union type'?

拜拜、爱过 提交于 2019-11-26 10:58:46
问题 Background Discussions on the mostly un-or-implementation-defined nature of type-punning via a union typically quote the following bits, here via @ecatmur ( https://stackoverflow.com/a/31557852/2757035 ), on an exemption for standard-layout struct s having a \"common initial sequence\" of member types: C11 ( 6.5.2.3 Structure and union members ; Semantics ): [...] if a union contains several structures that share a common initial sequence (see below), and if the union object currently

What does “request for member '*******' in something not a structure or union” mean?

笑着哭i 提交于 2019-11-26 10:27:22
问题 Is there an easy explanation for what this error means? request for member \'*******\' in something not a structure or union I\'ve encountered it several times in the time that I\'ve been learning C, but I haven\'t got a clue as to what it means. 回答1: It also happens if you're trying to access an instance when you have a pointer, and vice versa: struct foo { int x, y, z; }; struct foo a, *b = &a; b.x = 12; /* This will generate the error, should be b->x or (*b).x */ As pointed out in a

Can a union be initialized in the declaration?

丶灬走出姿态 提交于 2019-11-26 09:47:17
问题 For example, say we have a union typedef union { unsigned long U32; float f; }U_U32_F; When a variable of this union type is declared, is there a way to set an initial value? U_U32_F u = 0xffffffff; // Does not work...is there a correct syntax for this? 回答1: Use an initializer list: U_U32_F u = { 0xffffffff }; You can set other members than the first one via U_U32_F u = { .f = 42.0 }; 回答2: Try U_U32_F u = {0xffffffff}; 回答3: Note that per-member union initialization doesn't work on pre-C99

gcc, strict-aliasing, and casting through a union

狂风中的少年 提交于 2019-11-26 08:08:41
问题 Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union: [...] Taking the address, casting the resulting pointer and dereferencing the result has undefined behavior [emphasis added], even if the cast uses a union type, e.g.: union a_union { int i; double d; }; int f() { double d = 3.0; return ((union a_union *)&d)->i; } Does anyone have an example to illustrate this undefined behavior? Note this question

When would anyone use a union? Is it a remnant from the C-only days?

喜夏-厌秋 提交于 2019-11-26 06:11:41
问题 I have learned but don\'t really get unions. Every C or C++ text I go through introduces them (sometimes in passing), but they tend to give very few practical examples of why or where to use them. When would unions be useful in a modern (or even legacy) case? My only two guess would be programming microprocessors when you have very limited space to work with, or when you\'re developing an API (or something similar) and you want to force the end user to have only one instance of several

How to compile C code with anonymous structs / unions?

好久不见. 提交于 2019-11-26 04:09:44
问题 I can do this in c++/g++: struct vec3 { union { struct { float x, y, z; }; float xyz[3]; }; }; Then, vec3 v; assert(&v.xyz[0] == &v.x); assert(&v.xyz[1] == &v.y); assert(&v.xyz[2] == &v.z); will work. How does one do this in c with gcc? I have typedef struct { union { struct { float x, y, z; }; float xyz[3]; }; } Vector3; But I get errors all around, specifically line 5: warning: declaration does not declare anything line 7: warning: declaration does not declare anything 回答1: according to

sizeof a union in C/C++

烂漫一生 提交于 2019-11-26 03:40:35
问题 What is the sizeof the union in C/C++? Is it the sizeof the largest datatype inside it? If so, how does the compiler calculate how to move the stack pointer if one of the smaller datatype of the union is active? 回答1: The Standard answers all questions in section 9.5 of the C++ standard, or section 6.5.2.3 paragraph 5 of the C99 standard (or paragraph 6 of the C11 standard, or section 6.7.2.1 paragraph 16 of the C18 standard): In a union, at most one of the data members can be active at any

Why do we need C Unions?

谁都会走 提交于 2019-11-26 03:28:54
问题 When should unions be used? Why do we need them? 回答1: Unions are often used to convert between the binary representations of integers and floats: union { int i; float f; } u; // Convert floating-point bits to integer: u.f = 3.14159f; printf("As integer: %08x\n", u.i); Although this is technically undefined behavior according to the C standard (you're only supposed to read the field which was most recently written), it will act in a well-defined manner in virtually any compiler. Unions are

Why does C++ disallow anonymous structs?

心已入冬 提交于 2019-11-26 00:37:43
问题 Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It\'s a bit of syntactic sugar that\'s occasionally very helpful. What\'s the rationale that prevents this from being part of the standard? Is there a technical roadblock? A philosophical one? Or just not enough of a need to justify it? Here\'s a sample of what I\'m talking about: struct vector3 { union { struct { float x; float y; float z; }; float v[3]; }; }; My compiler will accept this, but it warns