| 109 | } |
| 110 | |
| 111 | bool is_inline() const { |
| 112 | // It is undefined behavior to access the inactive member of a |
| 113 | // union directly. However, it is well-defined to reinterpret_cast any |
| 114 | // pointer into a pointer to char and examine it as an array |
| 115 | // of bytes. See |
| 116 | // https://dev-discuss.pytorch.org/t/unionizing-for-profit-how-to-exploit-the-power-of-unions-in-c/444#the-memcpy-loophole-4 |
| 117 | bool result = false; |
| 118 | static_assert(offsetof(inline_array, is_inline) == 0, |
| 119 | "untagged union implementation relies on this"); |
| 120 | static_assert(offsetof(heap_vector, is_inline) == 0, |
| 121 | "untagged union implementation relies on this"); |
| 122 | std::memcpy(&result, reinterpret_cast<const char *>(this), sizeof(bool)); |
| 123 | return result; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | template <typename T, std::size_t InlineSize> |
no outgoing calls
no test coverage detected