1,716 questions
0 votes
1 answer
128 views
Is it well-formed to track POD class layouts at compile time?
I'm trying to generate compile-time info about my POD types. Getting the offset of each member has been challenging due to limitations like no pointer conversions in constexpr, but I think I've found ...
12 votes
4 answers
2k views
How does printing a union itself and not its member work in C?
I'm making a hashtable in C that I want to contain elements of different types, so I made it return a union that can hold int, float or char *. But when trying to print the union itself out, float ...
7 votes
0 answers
213 views
C++ floating point representation for big/little endian
I'm trying to make a union with a representation of an IEEE 32-bit floating point number. So far, I have this: #include <cstdint> struct FloatRepresentation{ union { float value; ...
0 votes
0 answers
95 views
vs2022 build complain union member not initialized when used in inheried class
I have a union declared in a class: class A { protected: union { uint16_t a[8] = { 0 }; uint64_t b[2]; }; } To test something in class A I created an extend class to ...
5 votes
1 answer
86 views
Implicit conversion from int to union works in designated initializer but otherwise fails to compile
I have a program that maps device registers to unions of bitfields and integers. This is part of a larger code base, so although this is very C-style code, we are compiling it as C++20. Here is a ...
0 votes
0 answers
164 views
How to create an efficient implementation for multiarraylist-like type for unions in zig?
I know MultiArrayList supports tagged union, but it does it by converting it into struct containing untagged union and a tag field, effectively creating 2 ArrayList fields, one for union and the other ...
-2 votes
1 answer
69 views
Why are anonymous unions not allowed in structured bindings similar to how they work in aggregate initialization?
In C++17 structured bindings, the object which is bound to cannot have anonymous unions. But in aggregate initialization, anonymous unions can exist in the aggregate being initialized, and the first ...
1 vote
2 answers
170 views
Is "container punning" defined behavior when using Unions
Having read a fair bit about the purpose of unions, and type-punning (that it's basically not allowed, and you should rely on the compiler optimizing away memcpy calls), I'm wondering if the following ...
3 votes
3 answers
321 views
Union-based type punning and mixed C/C++ code
I have a C codebase which uses unions for type punning. I am then unit testing this code in C++ with gmock. I know union-based type punning is illegal in C++, so wanted to make sure my usage is safe: /...
1 vote
0 answers
159 views
Why do I get a SIGILL when converting a uint64 to a double?
I link to the Jolt Physics library, and I can build my project no problems on Desktop. On Android I can install on my physical device through USB and debug it, and this error doesn't happen. However ...
17 votes
1 answer
723 views
Can non-const data be accessed through a const pointer in a union?
Given a union that contains const char* and char* members, can data set through the char* be safely accessed through the const char*, in C99 and newer versions? For example, does the following have ...
0 votes
1 answer
76 views
Add union of subclasses to vector of base class in C++
I have a base class A, and two subclasses B and C which inherit from A. I would like to be able to store a union of B and C in an std::vector<A*>, but it seems that it may not be possible. Here'...
-2 votes
1 answer
127 views
Is placement new in an union UB for SOO?
I am writing a simplified version of std::move_only_function in C++11. In my implementation, I provide Small Object Optimization (a.k.a SOO) for my code, which is basically a union that stores both a ...
2 votes
1 answer
121 views
How is memory allocated in my union of double and bit-fields struct?
I have a union of a double and a struct of bit fields: union byte_real { double db; struct { unsigned long long int mant : 52; unsigned int exp : 11; unsigned int sgn : ...
1 vote
2 answers
106 views
What are the prohibitions against certain kinds of aliasing through unions?
In the C standard (specifically, n3220, ie. the latest working draft of C23), which paragraphs would make the following code examples invoke undefined behaviour?: void f(int *a, char *b) { (*a)++; ...