unions

C Unions and Polymorphism [duplicate]

心不动则不痛 提交于 2019-12-05 02:10:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How can I simulate OO-style polymorphism in C? I'm trying to use unions to create polymorphism in C. I do the following. typedef struct{ ... ... } A; typedef struct{ ... ... } B; typedef union{ A a; B b; }C; My question is: how can I have a method that takes type C, but allows for A and B's also. I want the following to work: If I define a function: myMethod(C){ ... } then, I want this to work: main(){ A myA;

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

£可爱£侵袭症+ 提交于 2019-12-05 01:34:13
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 (using a union ). This way all of a , b and w.b will be true , but a and w.b won't be equal, so this

Why does my union not show the correct values?

一笑奈何 提交于 2019-12-05 01:14:46
union { int i; bool b; } x; x.i = 20000; x.b = true; cout << x.i; It prints out 19969. Why does it not print out 20000? A union is not a struct . In a union , all of the data occupies the same space and can be treated as different types via its field names. When you assign true to x.b , you are overwriting the lower-order bits of 20000 . More specifically: 20000 in binary: 100111000100000 19969 in binary: 100111000000001 What happened here was that you put a one-byte value of 1 (00000001) in the 8 lower-order bits of 200000. If you use a struct instead of a union , you will have space for both

Changing active member of union in constant expressions

自闭症网瘾萝莉.ら 提交于 2019-12-05 01:00:48
问题 Playing with constexpr and union I found out, that I can't change active member of an union in constexpr . Just one exception: union of empty classes. constexpr bool t() { struct A {}; struct B {}; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; return true; } static_assert(t()); constexpr bool f() { struct A { char c; }; struct B { char c; }; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; // error originating from here return true; } static_assert(f()); First function may produce constant

Shapeless: map from coproduct to different coproduct

只愿长相守 提交于 2019-12-05 00:55:31
In the following, I'm trying to make a polymorphic function to convert a RawFeatureValue into a RefinedFeatureValue . import shapeless._ object test { type RawFeatureValue = Int :+: Double :+: String :+: CNil type RefinedFeatureValue = Int :+: Double :+: CNil private object convert extends Poly1 { implicit def caseInt = at[Int](i => i) implicit def caseDouble = at[Double](d => d) implicit def caseString = at[String](s => s.hashCode) } val a = Coproduct[RawFeatureValue](12) val b: RefinedFeatureValue = a map convert } However, the resulting type is Int :+: Double :+: Int :+: CNil which is not

union versus void pointer

偶尔善良 提交于 2019-12-05 00:31:47
What would be the differences between using simply a void* as opposed to a union? Example: struct my_struct { short datatype; void *data; } struct my_struct { short datatype; union { char* c; int* i; long* l; }; }; Both of those can be used to accomplish the exact same thing, is it better to use the union or the void* though? I had exactly the case in our library. We had a generic string mapping module that could use different sizes for the index, 8, 16 or 32 bit (for historic reasons). So the code was full of code like that: if(map->idxSiz == 1) return ((BYTE *)map->idx)[Pos] = ...whatever

Nameless union inside a union

这一生的挚爱 提交于 2019-12-05 00:24:58
I'm reading some code and found something like the following: typedef union { int int32; int boolean; time_t date; char *string; union { struct foo *a; struct foo *b; struct foo *c; }; } type_t; From syntax point of view, the inner union {} can be removed and having *a, *b and *c directly inside the outer union {}. So what's the purpose the namelessly embedded union? Yu Hao Unnamed union/struct inside another union/struct is a feature of C11, and some compiler extensions (e.g, GCC ). C11 §6.7.2.1 Structure and union specifiers 13 An unnamed member whose type specifier is a structure specifier

Common initial sequence and alignment

懵懂的女人 提交于 2019-12-05 00:03:22
While thinking of a counter-example for this question , I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B ? struct B { char byte; }; Furthermore, if we have struct A { alignas(2) char x; alignas(4) char y; }; // possible alignment, - is padding // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 // x - - - y - - - x - - - y - - - struct B { char x; char y; }; // no padding required union U { A a; B b; } u; Is there a common initial sequence for A and B ? If so, does it include A::y & B::y ? I.e., may we write

what is size of empty class and difference between union, structure and class in c++ ?

淺唱寂寞╮ 提交于 2019-12-04 20:14:53
what is size of empty class and difference between union, structure and class in c++ ? My idea: if no static members in them, they should be same because all members are allocated on stack. If they are all empty, they are same. if they have static members, it depends the relative location of the members inside them. right ? thanks C++ Standard standard specify's that the size of an Empty class should be Non-Zero . Usually, it is 1 byte on most systems. In Bjarne Stroustrup's words, the size is non-zero " To ensure that the addresses of two different objects will be different." The size is 1 on

Is accessing bytes of a __m128 variable via union legal?

梦想的初衷 提交于 2019-12-04 19:02:38
问题 Consider this variable declaration: union { struct { float x, y, z, padding; } components; __m128 sse; } _data; My idea is to assign the value through x , y , z fields, perform SSE2 computations and read the result through x , y , z . I have slight doubts as to whether it is legal, though. My concern is alignment: MSDN says that __m128 variables are automatically aligned to 16 byte boundary, and I wonder if my union can break this behavior. Are there any other pitfalls to consider here? 回答1: