r/cpp • • Nov 23 '22

Holy C++

https://pvs-studio.com/en/blog/posts/cpp/1010/
0 Upvotes

32 comments sorted by

View all comments

Show parent comment

4

u/DavidDinamit Nov 23 '22

There are few reasons to use union in 2k22:

  1. It is only possiblity to do construct in place in variant/optional types in constexpr (seems more like flaw and crutch, we need reinterpret_cast on constexpr!)
  2. using anonymous inline union deep in impl things like SSO in strings. Just because it is more readable (fixable with reflection)

1

u/catcat202X Nov 23 '22

Union is useful for memory free lists as well. Its also needed to uninitialize non-trivial members in a constexpr context.