unions Questions

1

I am trying to better understand a rather surprising discovery regarding unions and the common initial sequence rule. The common initial sequence rule says (class.mem 23):  In a standard-layout ...
Puffin asked 27/4, 2017 at 10:50

2

Solved

Consider the following union: typedef union { struct { // Anonymous struct int red; int green; int blue; }; int colorChannels[3]; } Color; Regardless of the system this code is compiled o...
Hanson asked 22/2, 2018 at 23:1

1

Solved

Assume that you have to implement a static_vector<T, N> class, which is a fixed capacity container that entirely lives on the stack and never allocates, and exposes an std::vector-like interf...
Aim asked 6/2, 2018 at 14:22

3

Solved

I have declared this array of unions: union Function { char* name; double (*fnct)(); int args; }; union Function functions[] = { {.name = "acos", .fnct = acos, .args = 1}, {.name = "asin", ....
Sheilasheilah asked 26/1, 2018 at 12:20

1

Solved

In the union U below, if a or b is the active member, is it defined behavior to access c? struct A{ int a; }; struct B{ int a; double b; }; union U{ A a; B b; int c; }; In [class.union],...
Romanov asked 11/1, 2018 at 14:18

3

Solved

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 = ...
Demount asked 2/1, 2018 at 9:37

6

Solved

I am wondering if it is possible to use unions as arguments to a function: Let's say I have two structures: struct complex_attribute{ struct generic_attribute *sub_attributes[20]; }; struct gen...
Gpo asked 26/6, 2015 at 18:27

3

Solved

In other words, according to the C standard, is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU)...
Pro asked 22/11, 2017 at 9:7

18

I have learned but don't really get unions. Every C or C++ text I go through introduces them (sometimes in passing), but they tend to give very few practical examples of why or where to use them. W...
Vickyvico asked 25/1, 2011 at 1:0

4

Solved

I have the following: #include <stdio.h> typedef union u_data { struct { int a; int b; int c; }; int elem[3]; } my_data; int main(void) { my_data data; data.a = 3; data.b = 5; ...
Viviennevivify asked 24/11, 2017 at 10:36

1

Solved

What makes a union member active? I've read chapter 9.5 of the C++14 standard (the one about unions), but I haven't found a clear answer to what makes a union member active. There is a note: I...
Orth asked 7/11, 2017 at 22:19

1

Solved

I want to have a named union in the following struct so that I can memcpy it without knowing what field is "active". struct Literal { enum class Type : size_t { INT = 1, LONG, FLOAT, DOUBLE ...
Papa asked 31/10, 2017 at 9:37

2

Solved

Consider a union whose members share a common base class: struct Base { int common; }; struct DerivedA : Base {}; struct DerivedB : Base {}; union Union { DerivedA a; DerivedB b; }; No matt...
Heteropterous asked 9/10, 2017 at 2:12

1

Solved

I need to manage bitfield data and unions. Here is the code like I think it in C: typedef struct __attribute__((__packed__)){ union { struct __attribute__((__packed__)){ unsigned short protocol...
Estimate asked 6/10, 2017 at 9:42

2

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() { ...
Frodin asked 21/9, 2017 at 17:18

4

Solved

The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or it holds no value. sizeof(std::...
Dympha asked 8/8, 2017 at 18:47

2

Solved

I'd like to be able to generically pass a function to a function in C. I've used C for a few years, and I'm aware of the barriers to implementing proper closures and higher-order functions. It's al...
Toandfro asked 15/1, 2013 at 5:52

5

Solved

C++ 0x draft 9.5.6 Anonymous unions declared in a named namespace or in the global namespace shall be declared static. Why is this?
Koy asked 2/11, 2010 at 3:5

2

This code implements an unrestricted union which provides access by name and by index to any of its three members. Since std::string is non-trivially constructed and destroyed, I need to provide sp...
Chibouk asked 27/4, 2017 at 22:38

1

Consider the following types: struct A { int x; }; struct B { int y; char z; }; union U { A a; B b; }; And this code fragment: U u; new (&u.b) B; b.y = 42; b.z = 'x'; At this point, readi...
Radix asked 8/5, 2017 at 15:52

7

Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union: [...] Taking the address, casting the resulting...
Blocky asked 25/5, 2010 at 16:6

3

Solved

I am unsure of whether or not the code has pointer aliasing (or other standard conformance issues) in the asserts cast. It seems that a pointer to the union type should be able to be cast to a poin...
Docile asked 23/12, 2013 at 22:30

1

Solved

Normally you are responsible for lifetime of your unrestricted union members -- and typically you do it via in-place ctor/dtor calls. But, apparently, there is at least one case when compiler helps...
Hammurabi asked 22/4, 2017 at 22:48

5

Solved

I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important. typedef union{ uint32_t raw; struct{ unsigned int present:1; unsigned int rw:1; unsigned...
Cultus asked 11/6, 2009 at 2:37

3

Solved

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 :+: St...
Hirsutism asked 27/4, 2015 at 14:32

© 2022 - 2024 — McMap. All rights reserved.