32 enum EitherSide :
char { left, right };
37 template <
typename Left_t,
typename Right_t>
class EitherImpl {
42 template <
typename... Args>
44 : side(EitherSide::left) {
45 new (&storage) Left_t(std::forward<Args>(args)...);
51 template <
typename... Args>
53 : side(EitherSide::right) {
54 new (&storage) Right_t(std::forward<Args>(args)...);
67 new (&storage) Left_t(rhs.
asLeft());
69 new (&storage) Right_t(rhs.
asRight());
78 new (&storage) Left_t(std::move(rhs.asLeft()));
80 new (&storage) Right_t(std::move(rhs.asRight()));
97 new (&storage) Left_t((rhs.
asLeft()));
99 new (&storage) Right_t((rhs.
asRight()));
117 new (&storage) Left_t(std::move(rhs.asLeft()));
119 new (&storage) Right_t(std::move(rhs.asRight()));
131 assert(side == right);
132 return *
reinterpret_cast<Right_t*
>(&storage);
142 assert(side == right);
143 return *
reinterpret_cast<const Right_t*
>(&storage);
153 assert(side == left);
154 return *
reinterpret_cast<Left_t*
>(&storage);
164 assert(side == left);
165 return *
reinterpret_cast<const Left_t*
>(&storage);
179 asRight().~Right_t();
183 using storage_t =
typename std::aligned_union<1, Left_t, Right_t>::type;
Tag indicating the left side.
Definition: eitherImpl.hpp:11
EitherImpl & operator=(const EitherImpl &rhs)
Copy assignment operator; Copies underlying value.
Definition: eitherImpl.hpp:87
~EitherImpl()
Destructor, calling destructor of contained type.
Definition: eitherImpl.hpp:60
Tag indicating the right side.
Definition: eitherImpl.hpp:22
EitherSide side
Indicates whether this contains an A or a B.
Definition: eitherImpl.hpp:172
EitherImpl(RightSide, Args &&... args)
Construct containing right type.
Definition: eitherImpl.hpp:52
EitherImpl(LeftSide, Args &&... args)
Construct containing left type.
Definition: eitherImpl.hpp:43
const Left_t & asLeft() const
Returns stored A value.
Definition: eitherImpl.hpp:163
Implementation of (safe) union of two types.
Definition: eitherImpl.hpp:37
EitherImpl(const EitherImpl &rhs)
Copy constructor; Copies underlying value.
Definition: eitherImpl.hpp:65
EitherImpl(EitherImpl &&rhs) noexcept
Move constructor; Moves underlying value.
Definition: eitherImpl.hpp:76
Right_t & asRight()
Returns stored B value.
Definition: eitherImpl.hpp:130
Definition: either.hpp:10
const Right_t & asRight() const
Returns stored B value.
Definition: eitherImpl.hpp:141
Left_t & asLeft()
Returns stored A value.
Definition: eitherImpl.hpp:152
EitherImpl & operator=(EitherImpl &&rhs) noexcept
Move assignment operator; Moves underlying value.
Definition: eitherImpl.hpp:107