unions

using a union-like class in an std::initializer_list

大城市里の小女人 提交于 2019-12-12 04:10:01
问题 In the code below I show union-like class S which contains two non-related structs B and C. I show how to instantiate the non-POD std::string and delete it again and then switch S to S::CC and set the num int. #include <vector> #include <string> #include <iostream> #include <memory> struct B { B() {} ~B() {} std::string str; void Func1() {} }; struct C { C() {} ~C() {} int num; void Func2() {} }; struct S { S() { tag = CC; } S( const S& s ) { switch( s.tag ) { case BB: new ( &b.str ) std:

Bit fields in a union - how portable is this?

送分小仙女□ 提交于 2019-12-12 01:11:14
问题 I got a bit field with a bunch of flags, and I need a quick and dirty way to set everything to zero, so instead of blindly casting the struct to an integer, I decided it would be "better" to put the bit fields in a union with an actual integer. union Flags { uint _all; struct { uint status : 2; uint expanded : 1; uint draw : 1; uint drawChildren : 1; uint hidden : 1; uint disabled : 1; uint used : 1; uint deletable : 1; uint incomplete : 1; uint isStatic : 1; uint isConst : 1; uint isVolatile

UNION NULLS into View that can be queried later

那年仲夏 提交于 2019-12-11 17:23:28
问题 Microsoft SQL SERVER: I am working on a skills matrix problem. The example below is a simplified scenario. A company has a factory with two job titles: Apprentice (APP) and Expert (EXP). You can see in the jobskills table that Apprentices must be able to Cut, Drill, and Bend (10, 20, 30). Experts should be able to Cut, Drill, Bend, Weld, and Turn on a lathe (10 thru 50). jobskills table: job_code skill_desc skill_ID -------- ---------- -------- APP Cut 10 APP Drill 20 APP Bend 30 EXP Cut 10

Explain the result of sizeof operator for a union containing structures

不打扰是莪最后的温柔 提交于 2019-12-11 16:26:27
问题 #include<stdio.h> struct mystruct { char cc; float abc; }; union sample { int a; float b; char c; double d; struct mystruct s1; }; int main() { union sample u1; int k; u1.s1.abc=5.5; u1.s1.cc='a'; printf("\n%c %f\n",u1.s1.cc,u1.s1.abc); k=sizeof(union sample); printf("%d\n\n",k); return 0; } The size of operator is returning 8 I am still able to access the structure elements, more than one at a time and still the sizeof operator is returning the max size of primitive data types i assume. Why

Resolving a compiler error due to an invariant member with a possible deleted default constructor

青春壹個敷衍的年華 提交于 2019-12-11 14:55:47
问题 I have asked a series of questions that all relate to the same source code in this order: experimenting-with-unions-and-bitfields-within-a-structures-and-templates trying-to-flip-the-order-of-bits-in-stdbitset avoiding-ambiguity-in-overload-resolution I've also asked these series of questions over at Code Review that are also related. emulating-virtual-registers-by-experimenting-with-unions-bitfields-structs-and-template-specialization emulating-virtual-registers-part-2 This should give you

Curious segfault when using Python CTypes union with 4 structs but not less

时光总嘲笑我的痴心妄想 提交于 2019-12-11 13:08:37
问题 I'm running into a curious segfault when trying to interface to a library using Python CTypes. I'm running Python 3.4.0-0ubuntu2 on Kubuntu Trusty 64 bit. The segfault occurs when I use 4 anonymous struct s within a union but not when I use 3 or less, which is most curious. It also occurs only when I try to return a union back to Python in fn2 but not when I just send it from Python in fn1 . The code with 3 struct s which does not segfault: lib.c: #include <stdio.h> typedef union { int data[3

Arduino: cannot pass union struct as pointer ac I can with gcc compiler

我怕爱的太早我们不能终老 提交于 2019-12-11 12:32:38
问题 I'm trying to use structs with Arduino but cannot pass a struct pointer with a function call. Everything works fine when adding a main function and compiling for my computer with gcc but with the Arduino IDE I get errors. The code I tried was: typedef union { struct { unsigned unit :2; unsigned channel:2; unsigned status :1; unsigned group :1; unsigned remote :26; }; unsigned long data; } Signal; Signal signal; void testPassingStruct(Signal *variable) { variable->status = 1; } void setup() {

Compilation error for union

会有一股神秘感。 提交于 2019-12-11 11:22:27
问题 can somebody please explain why the following program causing the compilation problem. I have compiled the source code over VS2013. #include <iostream> using namespace std; // Do not work union myuni { string str; }; void main() { } Does union require the fixed length size while declaring it? The same scenario works fine with structure. 回答1: You cannot have a string in a union as the former contains a constructor. (Although allowed in C++11 this is not supported in VS2013). 来源: https:/

How to initialize a non-POD member in Union

非 Y 不嫁゛ 提交于 2019-12-11 10:29:15
问题 In c++11, Union supports non-POD member. I want to initialize a non-POD member in the constructor. On wikipedia c++11 page, it uses a placement 'new' to initialize a non-POD member. #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, a constructor definition is now required. }; I am wondering is

C++ understanding Unions and Structs

浪尽此生 提交于 2019-12-11 10:17:37
问题 I've come to work on an ongoing project where some unions are defined as follows: /* header.h */ typedef union my_union_t { float data[4]; struct { float varA; float varB; float varC; float varD; }; } my_union; If I understand well, unions are for saving space, so sizeof(my_union_t) = MAX of the variables in it. What are the advantages of using the statement above instead of this one: typedef struct my_struct { float varA; float varB; float varC; float varD; }; Won't be the space allocated