Things like this are technically UB in C++17: https://github.com/bitcoin/bitcoin/blob/4766cd198172225c66a0adb01f6cb9513c3d0e66/src/prevector.h#L187
- You cannot start the lifetime of an array like this. You need to do some compiler magic (such as perhaps call
std::launder? Or actually some weird placement-new that does nothing?) here to properly start the lifetime of theTarray object, as far as the C++ abstract machine is concerned. See: https://www.youtube.com/watch?v=pbkQG09grFw- C++20 does now allows this -- implicit lifetimes can be started when you use some backing store in this way for trivial types only, but in C++17 this is technically UB.
- The fact that this works is a happy accident because most major compiler support such (mis)-use of the language for trivial types owing to its C roots. So much so that they actually changed the standard for C++20 to support this, but for '17 this is UB (but works in practice on all major compilers).
- The following is not fixed in C++20 though: Memory is not guaranteed to be aligned to whatever T requires here. (This is not fixed by C++20 -- unaligned access is UB, even if it happens to work on your platform). So
prevectoronly really works without UB foruint8_tand similar types, if on C++20.