unions

Why do unions have a deleted default constructor if just one of its members doesn't have one?

雨燕双飞 提交于 2019-11-27 14:49:43
问题 N3797::9.5/2 [class.union] says: If any non-static data member of a union has a non-trivial default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4), the corresponding member function of the union must be user-provided or it will be implicitly deleted (8.4.3) for the union I was trying to understand that note by example: #include <iostream> #include <limits> struct A { A(const A&){ std:

Examples of Union in C [closed]

怎甘沉沦 提交于 2019-11-27 11:28:35
I'm looking for some union examples, not to understand how union works, hopefully I do, but to see which kind of hack people do with union. So feel free to share your union hack (with some explanation of course :) ) One classic is to represent a value of "unknown" type, as in the core of a simplistic virtual machine: typedef enum { INTEGER, STRING, REAL, POINTER } Type; typedef struct { Type type; union { int integer; char *string; float real; void *pointer; } x; } Value; Using this you can write code that handles "values" without knowing their exact type, for instance implement a stack and so

C: Where is union practically used?

大憨熊 提交于 2019-11-27 09:51:44
问题 I have a example with me where in which the alignment of a type is guaranteed, union max_align . I am looking for a even simpler example in which union is used practically, to explain my friend. 回答1: I usually use unions when parsing text. I use something like this: typedef enum DataType { INTEGER, FLOAT_POINT, STRING } DataType ; typedef union DataValue { int v_int; float v_float; char* v_string; }DataValue; typedef struct DataNode { DataType type; DataValue value; }DataNode; void myfunct()

union for uint32_t and uint8_t[4] undefined behavior? [duplicate]

耗尽温柔 提交于 2019-11-27 09:09:42
This question already has an answer here: Purpose of Unions in C and C++ 15 answers In the comments of this answer it is said that it would be undefined behavior to split up an integer into their bytes using a union like follows. The code given at that place is similar though not identical to this, please give a note if have I changed undefined-behavior-relevant aspects of the code. union addr { uint8_t addr8[4]; uint32_t addr32; }; Up to now I thought this would be a fine approach to do things like addr = {127, 0, 0, 1}; and get the corresponding uint32_t in return. (I acknowledge that this

Assigning two values with an union variable

≯℡__Kan透↙ 提交于 2019-11-27 08:26:13
问题 The variable a is assigned by value 10 and the variable b is assigned by 20 with the union variable v. Then it gives the output of a is 20 instead of 10. I don't get it. #include<stdio.h> int main() { union var { int a, b; }; union var v; v.a=10; v.b=20; printf("%d\n", v.a); return 0; } I executed the program and I got 20 as output. 回答1: union overlays all the listed members on top of each other (although some may extend farther than the overlapping initial sections), so assigning to either

C++11 anonymous union with non-trivial members

感情迁移 提交于 2019-11-27 07:06:30
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, U will have an implicitly deleted default constructor, copy constructor, copy assignment operator, move

Do unrestricted unions require placement new and a constructor definition?

不想你离开。 提交于 2019-11-27 06:57:35
问题 The examples I've seen of unrestricted unions always seem to use placement new when constructing. The Wikipedia article for C++11 features uses placement new in the constructor of a union. https://en.wikipedia.org/wiki/C%2B%2B11#Unrestricted_unions #include <new> // Required for placement 'new'. struct Point { Point() {} Point(int x, int y): x_(x), y_(y) {} int x_, y_; }; union U { int z; double w; Point p; // Illegal in C++03; legal in C++11. U() {new(&p) Point();} // Due to the Point member

Access struct members as if they are a single array?

荒凉一梦 提交于 2019-11-27 05:11:56
I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call; } quantities; And then I use them to calculate: average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call; Every now and them I need to include another variable. Now, for instance, I need to include v_clean and qtt_clean . I can't change the structures to arrays: typedef

Anonymous union within struct not in c99?

陌路散爱 提交于 2019-11-27 04:27:33
here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node int_n; struct double_node double_n; }; }; int main(void) { struct int_node i; i.value = 10; struct node n; n.type = t_int; n. int_n = i; return 0; } And what I don't undestand is this: $ cc us.c $ cc -std=c99 us.c us.c:18:4: warning: declaration does not declare anything us.c: In function ‘main’: us.c:26:4: error: ‘struct node’ has no member named ‘int_n’ Using GCC without -std option

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

我的梦境 提交于 2019-11-27 03:57:51
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 contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere