unions

Could someone explain this C++ union example?

狂风中的少年 提交于 2019-12-19 04:00:35
问题 I found this code on cppreference.com. It's the strangest C++ I've seen, and I have a few questions about it: union S { std::string str; std::vector<int> vec; ~S() {} }; int main() { S s = { "Hello, world" }; // at this point, reading from s.vec is undefined behavior std::cout << "s.str = " << s.str << '\n'; s.str.~basic_string<char>(); new (&s.vec) std::vector<int>; // now, s.vec is the active member of the union s.vec.push_back(10); std::cout << s.vec.size() << '\n'; s.vec.~vector<int>(); }

Could someone explain this C++ union example?

烂漫一生 提交于 2019-12-19 04:00:03
问题 I found this code on cppreference.com. It's the strangest C++ I've seen, and I have a few questions about it: union S { std::string str; std::vector<int> vec; ~S() {} }; int main() { S s = { "Hello, world" }; // at this point, reading from s.vec is undefined behavior std::cout << "s.str = " << s.str << '\n'; s.str.~basic_string<char>(); new (&s.vec) std::vector<int>; // now, s.vec is the active member of the union s.vec.push_back(10); std::cout << s.vec.size() << '\n'; s.vec.~vector<int>(); }

Exception to strict aliasing rule in C from 6.5.2.3 Structure and union members

梦想与她 提交于 2019-12-19 03:11:50
问题 Quote from C99 standard: 6.5.2.3 5 One special guarantee is made in order to simplify the use of unions: 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 that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types

Copying (using assignment) a structure to a structure inside a union causing seg fault

耗尽温柔 提交于 2019-12-18 09:34:52
问题 I wrote the following code: #include <iostream> #include <string> #include <cstring> struct bar { std::string s3; std::string s4; }Bar; union foo { char * s1; char * s2; bar b1; foo(){}; ~foo(){}; }Foo; int main () { foo f1; bar b2; std::string temp("s3"); b2.s3 = temp; b2.s4 = temp; //f1.b1 = b2; //-- This Fails (Seg faults) /* #0 0x00002b9fede74d25 in std::string::_Rep::_M_dispose(std::allocator<char> const&) [clone .part.12] () from /usr/local/lib64/libstdc++.so.6 #1 0x00002b9fede75f09 in

How to write destructor for union-like class

▼魔方 西西 提交于 2019-12-18 05:56:37
问题 I'm trying to use an union (C++) that has some non-primitive variables, but I'm stuck trying to create the destructor for that class. As I have read, it is not possible to guess what variable of the union is being used so there is no implicit destructor, and as I'm using this union on the stack, the compiler errors that the destructor is deleted. The union is as follows: struct LuaVariant { LuaVariant() : type(VARIANT_NONE) { } LuaVariantType_t type; union { std::string text; Position pos;

Is it valid to use bit fields with union?

China☆狼群 提交于 2019-12-18 05:53:46
问题 I have used bit field with a structure like this, struct { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; Now i wondered to see if this can be done with a union so i modified the code like, union { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; I found the bit field with union works but all those fields in the union are given to a single bit as I understood from output. Now I am seeing it is not erroneous to use

What are the benefits of unnamed structs / unions in C?

落花浮王杯 提交于 2019-12-18 05:47:12
问题 I found one code implemented as the similar demo shown below .. struct st { int a; struct { int b; }; }; 6.58 Unnamed struct/union fields within structs/unions As permitted by ISO C11 . But What are benefits of it ? Because anyway I can access the data members in a same manner like int main() { struct st s; s.a=11; s.b=22; return 0; } compiled on gcc 4.5.2 with , gcc -Wall demo.c -o demo and no errors , 回答1: It does not have to be an anonymous struct inside a struct, which I do not find very

Do union types actually exist in python?

巧了我就是萌 提交于 2019-12-17 19:35:06
问题 Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else "s" But is the way python was actually intended to be used? or in other words, do union types exist in the sense they do in racket for example? Or do we only use them like this: def f(x): if x: return "x" where the only "union" we need is with None? 回答1: Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple

Why aren't bitfields allowed with normal variables?

百般思念 提交于 2019-12-17 19:26:25
问题 I wonder why bitfields work with unions/structs but not with a normal variable like int or short . This works: struct foo { int bar : 10; }; But this fails: int bar : 10; // "Expected ';' at end of declaration" Why is this feature only available in unions/structs and not with variables? Isn't it technical the same? Edit: If it would be allowed you could make a variable with 3 bytes for instance without using the struct/union member each time. This is how I would to it with a struct: struct

memcpy/memmove to a union member, does this set the 'active' member?

冷暖自知 提交于 2019-12-17 18:08:07
问题 Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy , it copies from the address of a plain old uint32_t , which is not contained within a union. Also, I am copying (via memcpy ) to a specific member of a union ( u.a16 or &u.x_in_a_union , not directly to the entire union itself ( &u ) C++ is quite strict about unions - you should read from a member only if that was the last member that was written to: 9.5 Unions [class.union] [