unions Questions
1
Regarding this code:
#include <string>
int main()
{
union u {
u() { i = 0; }
~u() {}
int i;
std::string s1;
std::string s2;
} u;
new (&u) std::string{};
}
[intro.object]/2 s...
Kravits asked 17/1, 2019 at 13:25
7
1
Solved
I want to create a Discriminated Union Type, where it isn't required to pass the discriminator value.
Here's my current code:
interface Single<T> {
multiple?: false // this is optional, be...
Sil asked 3/1, 2019 at 22:43
1
Solved
I came to C from high-level Scala language and came up with a question. In Scala we usually handle error/exceptional conditions using Either which looks like the following:
sealed abstract class E...
1
My story starts off the same as this person's here:
Unions in C++11: default constructor seems to be deleted
The resolution here (now about three years old) is a bit unsatisfactory, because the "...
Abney asked 15/11, 2018 at 0:15
1
Solved
Is it legal to have fields with the same name across different anonymous unions inside one union?
union Foo
{
union
{
int bar;
};
union
{
int bar;
};
};
This code fails to compile by GC...
Auberge asked 14/11, 2018 at 22:38
2
In the c++ standard, in [basic.lval]/11.6 says:
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined...
Bobbie asked 5/11, 2018 at 8:51
1
Solved
This is the relationship I am talking about:
struct A{
int i = 1;
};
struct B{
union{A a;};
};
void main(){
B b;
};
In this constellation, my compiler (vs2015) complains about the default c...
Humanism asked 4/11, 2018 at 17:8
1
Look at this code:
struct A {
short s;
int i;
};
struct B {
short s;
int i;
};
union U {
A a;
B b;
};
int fn() {
U u;
u.a.i = 1;
return u.b.i;
}
Is it guaranteed that fn() returns 1?
...
Bunt asked 29/10, 2018 at 17:45
1
C++11 gaves us the ability to create anonymous unions with non-trivial members. That can be very useful sometimes - for example, if I want to create Holder class for some non-trivial object without...
3
When I read ISO/IEC 9899:1999 (see:6.5.2.3), I saw an example like this (emphasis mine) :
The following is not a valid fragment (because the union type is not visible within function f):
struct...
2
Solved
While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows:
template <typename T>
class optional {
// ...
...
Gritty asked 14/9, 2018 at 19:34
5
Solved
The following is not undefined behavior in modern C:
union foo
{
int i;
float f;
};
union foo bar;
bar.f = 1.0f;
printf("%08x\n", bar.i);
and prints the hex representation of 1.0f.
However th...
Sodamide asked 12/9, 2018 at 8:11
1
Solved
Using std::launder to get a pointer to an active object member from a pointer to an inactive object?
This question followes this one
Let's consider this example code:
struct sso
{
union{
struct {
char* ptr;
char size_r[8];
} large_str;
char short_str[16];
};
bool is_short_str() const...
Crusado asked 10/1, 2018 at 13:55
3
Solved
The following example is given in the C11 standard, 6.5.2.3
The following is not a valid fragment (because the union type is not
visible within function f):
struct t1 { int m; };
struct t2 { i...
4
Not sure if there is a term for this, "choice" seems to work. I'm working in C++, and I have a bunch of unions I need to create where the union represents a choice of one of the members of the unio...
10
Solved
From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlyin...
4
Important clarification: some commenters seem to think that I am copying from a union. Look carefully at the memcpy, it copies from the address of a plain old uint32_t, which is not contained withi...
Phosphatase asked 29/9, 2016 at 7:2
6
Solved
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 T...
2
Solved
GCC complains about this code even though I compile with -std=c++11 flag, and my gcc version supposedly supports Unrestricted unions (>4.6).
union
{
struct
{
float4 I,J,K,T;
};
struct
{
...
2
Solved
Quote from C99 standard:
6.5.2.3
5 One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), an...
Bultman asked 17/1, 2014 at 0:44
1
Solved
I can't seem to create a union where a member is or contains a glm::vec3 object (an object for representing a coordinate, containing 3 floats in this case). (source code for glm::vec)
It's used in...
1
Solved
I recently read a description of default constructors for unions:
Default Constructor
There is a following rule:
Blockquote
Deleted implicitly-declared default constructor:
[...] T is a unio...
Jasun asked 18/5, 2018 at 6:56
1
Solved
I implemented a tagged union using a class containing an anonymous union and a tag:
class LogFile
{
public:
LogFile(std::ostream& stdStream);
LogFile(std::ofstream fileStream);
LogFile(Log...
Liven asked 1/5, 2018 at 10:44
1
Solved
C++11 allows an anonymous union to be defined in a function, and its members can be accessed as variables of the function. If I examine the pointers to the different members, they are the same, but...
© 2022 - 2024 — McMap. All rights reserved.