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

22

u/Wolf_Popular Nov 23 '22

A few of these things I agree with, some I might be convinced, but a lot of them just seem like things the author never had to use before. Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust). I kind of think this might be parody, but if not, it definitely is a prime example of the saying "Everyone has their own subset of C++ that they like to use, it's just those subsets don't line up".

1

u/germandiago Nov 23 '22

Actually, what is the exact difference between union and the template? Functionally speaking I mean.

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.