unions

Unions in C++11: default constructor seems to be deleted

我的未来我决定 提交于 2019-12-04 17:43:35
问题 I am trying to understand how unions were extended by C++11. One thing that changed is the ability to use now non-static data members with non-trivial special member functions. From cppreference.com If a union contains a non-static data member with a non-trivial special member function (default constructor, copy/move constructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer. At most one data member

How to initialize a union? [duplicate]

霸气de小男生 提交于 2019-12-04 16:24:52
问题 This question already has answers here : Can a union be initialized in the declaration? (3 answers) Closed 4 years ago . If it's a struct then it can be done *p = {var1, var2..}; But seems this doesn't work with union : union Ptrlist { Ptrlist *next; State *s; }; Ptrlist *l; l = allocate_space(); *l = {NULL}; Only to get: expected expression before ‘{’ token 回答1: In C99, you can use a designated union initializer: union { char birthday[9]; int age; float weight; } people = { .age = 14 }; In C

Actual usage of union in C [duplicate]

≯℡__Kan透↙ 提交于 2019-12-04 14:36:56
This question already has answers here : Closed 8 years ago . Possible Duplicate: C: Where is union practically used? I know the concept of union but I don't see the actual case in real world coding that I should use union data structure. I will be very appreciated if you guys can give me some examples or tutorials that shows cases using union properly. Thanks in advance. Where I use it constantly: parsing a configuration file I store all values in a union data type. E.g. when values can be int types or strings, I would use a data structure as follows: struct cval_s { short type; union { int

Strange Behaviour Class Objects Inside Union

荒凉一梦 提交于 2019-12-04 12:37:29
问题 Hi I wanted know the reasons of the following code void main() { class test { public: test(){} int k; }; class test1 { public: test1(){} int k; }; union Test { test t1; test1 t2; }; } For the Above code it gives error " error C2620: union 'Test' : member 't1' has user-defined constructor or non-trivial default constructor " class test { public: //test(){} int k; }; class test1 { public: //test()1{}; int k; }; union Test { test t1; test1 t2; }; For the Above, No Errors. I wanted to know the

Is it legal and well defined behavior to use a union for conversion between two structs with a common initial sequence (see example)?

[亡魂溺海] 提交于 2019-12-04 10:10:15
问题 I have an API with a publicly facing struct A and an internal struct B and need to be able to convert a struct B into a struct A. Is the following code legal and well defined behavior in C99 (and VS 2010/C89) and C++03/C++11? If it is, please explain what makes it well-defined. If it's not, what is the most efficient and cross-platform means for converting between the two structs? struct A { uint32_t x; uint32_t y; uint32_t z; }; struct B { uint32_t x; uint32_t y; uint32_t z; uint64_t c; };

How to know that which variable from Union is Used?

萝らか妹 提交于 2019-12-04 08:06:44
问题 If I declare a Union as: union TestUnion { struct { unsigned int Num; unsigned char Name[5]; }TestStruct; unsigned char Total[7]; }; Now, How can I know that whether Total[7] is used or TestStruct is used? I am using C! I was revisiting unions and structures and this question came to my mind. " sizeof " can't be used as both are of same size i.e. 7 bytes. (And Here comes another question) When I filled only "Total" with a Character 'a' and Tried sizeof(TestUnionInstance) , it returned 12

How can I prevent a nameless struct\\union?

有些话、适合烂在心里 提交于 2019-12-04 07:26:52
I am building a class that has a union for its matrix data, however, I can only get it compile when I do not have a name for the struct\union. However, with a higher level warning level (four on visual studio) I will a warning saying warning C4201: nonstandard extension used : nameless struct/union I looked into it, and I don't seem to be able to find a way to prevent this. Anyway possible that I know of will cause a different compiler error related to the declaration of one or the other. How can I prevent getting this warning and make it conform to standards, without just disabling the

Why can't anonymous unions contain members with non-trivial constructors/destructors?

北战南征 提交于 2019-12-04 07:26:46
I may be mistaken, but the basic explanation I've found has been that the union can't initialize because it doesn't know which member's constructor to call. The compiler can not automatically generate a constructor for the union. Why is the user not allowed to define the unions constructor? This would remove said issue and allow the presence of union members that have a non-trivial constructor/destructor. Also, why can't a union member have any custom constructors? The previous explanation doesn't stand for custom constructors. Update 1: Example: struct SQuaternion { union { S3DVector Axis;

Getting Union, Intersection, or Difference of Sets in C++

房东的猫 提交于 2019-12-04 05:11:47
I have a couple questions about how to use C++ sets (std::set) Is there a way to get the union, intersection, or difference of two C++ sets? (It's pretty easy to write my own functionto do that but I wanted to know if there was a built in function for it) Can C++ sets be used as keys in a map? Use the set_difference() , set_union() , set_intersection() and set_symmetric_difference() functions. Sets and maps support any key type that can compare. By default this means the type has operator<() defined, but you can provide your own comparator. C++ sets don't have operator<() defined and therefore

Use of Union with reference

China☆狼群 提交于 2019-12-04 05:03:32
At work I've been using linux and the GCC compiler for C++11 and C++14. In some of the code at work, I've used a union to store both a reference and a pointer, as so: (Simplified to just the important parts) struct MyStruct { //Stuff union { double& x; double* x_ptr; }; MyStruct(double& value) : x(value) {} //More stuff }; I believe this code is clear, readable, unambiguous, and provides a convenient way to store references which can be shifted to something else. It provides easily understandable syntactic sugar without costing performance while improving readability. When I attempted to use