unions

Are there any guarantees for unions that contain a wrapped type and the type itself?

有些话、适合烂在心里 提交于 2019-11-28 03:02:16
问题 Can I put a T and a wrapped T in an union and inspect them as I like? union Example { T value; struct Wrapped { T wrapped; } wrapper; }; // for simplicity T = int Example ex; ex.value = 12; cout << ex.wrapper.wrapped; // ? The C++11 standards only guarantee save inspection of the common initial sequence, but value isn't a struct . I guess the answer is no , since wrapped types aren't even guaranteed to be memory compatible to their unwrapped counterpart and accessing inactive members is only

Is a Union Member's Destructor Called

╄→гoц情女王★ 提交于 2019-11-27 23:31:04
C++11 allowed the use of standard layout types in a union : Member of Union has User-Defined Constructor My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope? My understanding is that we must manually destroy and construct when switching: http://en.cppreference.com/w/cpp/language/union#Explanation But what about an example like this: { union S { string str; vector<int> vec; ~S() {} } s = { "Hello, world"s }; } When s goes out of scope, have I leaked the memory the string allocated on the heap because I did not call string 's destructor?

C++ unions vs. reinterpret_cast

对着背影说爱祢 提交于 2019-11-27 22:49:07
It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the integer value of 0xffff and literally interpret it as a series of bits in IEEE 754 floating point. ( Binary convert shows visually how this is done. ) #include <iostream> using namespace std; union unionType { int myInt; float myFloat; }; int main() { int i = 0xffff; unionType u; u.myInt = i; cout << "size of int " << sizeof(int) << endl; cout << "size

union of structs sharing same first members

十年热恋 提交于 2019-11-27 22:15:15
问题 I have been looking into an un-traditional way of achieving struct "polymorphism" in pre-C11 C. Let's say we have 2 structs: struct s1 { int var1; char var2; long var3; }; struct s2 { int var1; char var2; long var3; char var4; int var5; }; On most compilers, we could safely cast between pointers to the two and then access the common first members if no padding takes place. However, this is not standartized behaviour. Now, I found the following line in the C standard as far as C89: One special

Padding in union is present or not

一曲冷凌霜 提交于 2019-11-27 21:48:02
问题 Hello all, I want to know whether union uses padding? since the size of union is the largest data member size, can there be padding at the end? 回答1: since the size of union is the largest data member size That need not be true. Consider union Pad { char arr[sizeof (double) + 1]; double d; }; The largest member of that union is arr . But usually, a double will be aligned on a multiple of four or eight bytes (depends on architecture and size of double ). On some architectures, that is even

strict aliasing and alignment

偶尔善良 提交于 2019-11-27 18:50:28
I need a safe way to alias between arbitrary POD types, conforming to ISO-C++11 explicitly considering 3.10/10 and 3.11 of n3242 or later. There are a lot of questions about strict aliasing here, most of them regarding C and not C++. I found a "solution" for C which uses unions, probably using this section union type that includes one of the aforementioned types among its elements or nonstatic data members From that I built this. #include <iostream> template <typename T, typename U> T& access_as(U* p) { union dummy_union { U dummy; T destination; }; dummy_union* u = (dummy_union*)p; return u-

How do boost::variant and boost::any work?

∥☆過路亽.° 提交于 2019-11-27 17:06:16
How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded assignment operators. I queried the size of any and variant, and did some experiments with them. In my platform, variant takes the size of its longest possible type plus 8 bytes: I think it my just be 8 bytes o type information and the rest being the stored value. On the other hand, any just takes 8 bytes. Since i'm on a 64-bit platform, I guess any

Appending/concatenating two IEnumerable sequences

人走茶凉 提交于 2019-11-27 15:27:56
问题 I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I'm sure this is doable. I don't want to do a for loop and noticed that there is a Union method and a Join method on the two Lists. Any ideas? 回答1: Assuming your objects are of the same type, you can use either Union or Concat . Note that, like the SQL UNION keyword, the Union operation will ensure that duplicates are eliminated, whereas Concat (like UNION ALL ) will simply add

Union element alignment

安稳与你 提交于 2019-11-27 15:27:30
If I have a union, C standard guarantees that the union itself will be aligned to the size of the largest element. union U { long l; int i; short s; char c[2]; } u; But what does it say about alignment of individual union elements inside the union? Is the following expression guaranteed to be true? (&u.l == &u.i) && (&u.i == &u.s) && (&u.s == &u.c[0]) The start of each element is aligned with the address of the union itself. so the individual comparisons in the expression you ask about are true, but the expression as a whole is false unless the union is located at address 0x0001. The deleted

Is using an union in place of a cast well defined?

泄露秘密 提交于 2019-11-27 15:04:14
I had a discussion this morning with a colleague regarding the correctness of a "coding trick" to detect endianness. The trick was: bool is_big_endian() { union { int i; char c[sizeof(int)]; } foo; foo.i = 1; return (foo.c[0] == 1); } To me, it seems that this usage of an union is incorrect because setting one member of the union and reading another is not well-defined. But I have to admit that this is just a feeling and I lack actual proofs to strengthen my point. Is this trick correct ? Who is right here ? Your code is not portable. It might work on some compilers or it might not. You are