> For the complete documentation index, see [llms.txt](https://alex-robenko.gitbook.io/comms-protocols-cpp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alex-robenko.gitbook.io/comms-protocols-cpp/head-5/e.md).

# Appendix E - AlignedUnion

Implementation of `AlignedUnion` type.

```cpp
template <typename TType, typename... TTypes>
class AlignedUnion
{
    using OtherStorage = typename AlignedUnion<TTypes...>::Type;
    static const std::size_t OtherSize = sizeof(OtherStorage);
    static const std::size_t OtherAlignment = std::alignment_of<OtherStorage>::value;
    using FirstStorage = typename AlignedUnion<TType>::Type;
    static const std::size_t FirstSize = sizeof(FirstStorage);
    static const std::size_t FirstAlignment = std::alignment_of<FirstStorage>::value;
    static const std::size_t MaxSize = FirstSize > OtherSize ? FirstSize : OtherSize;
    static const std::size_t MaxAlignment = 
        FirstAlignment > OtherAlignment ? FirstAlignment : OtherAlignment;
public:

    /// Type that has proper size and proper alignment to keep any of the
    /// specified types
    using Type = typename std::aligned_storage<MaxSize, MaxAlignment>::type;
};

template <typename TType>
class AlignedUnion<TType>
{
public:
    using Type = 
        typename std::aligned_storage<sizeof(TType), std::alignment_of<TType>::value>::type;
};
```
