What's the purpose of using a union with only one member?
Because tx_side
is a union, tx_side()
doesn't automatically initialize/construct a
, and ~tx_side()
doesn't automatically destruct it.
This allows a fine-grained control over the lifetime of a
and pending_fifo
, via placement-new and manual destructor calls (a poor man's std::optional
).
Here's an example:
#include <iostream>
struct A
{
A() {std::cout << "A()\n";}
~A() {std::cout << "~A()\n";}
};
union B
{
A a;
B() {}
~B() {}
};
int main()
{
B b;
}
Here, B b;
prints nothing, because a
is not constructed nor destructed.
If B
was a struct
, B()
would call A()
, and ~B()
would call ~A()
, and you wouldn't be able to prevent that.