5 ms·
That's one of the situations for std::launder(). (However, in most cases where the new object has exactly the same type, C++20 has made laundering unnecessary w
by LegionMammal978 3y ago
That's one of the situations for std::launder(). (However, in most cases where the new object has exactly the same type, C++20 has made laundering unnecessary with its 'transparently replaceable' criterion.)
Yet the other situation is precisely this one [0] [1]. The byte array does provide storage for the T, but a pointer to the byte array is not a pointer to the T. You need to launder the pointer to get from the former to the latter. Such are the rules when you're bound as strictly to type-based aliasing as standard C++ is.
(Not that any real compiler is nearly as strict about byte arrays in practice: if they were, then many Unix networking APIs would be practically unusable. Indeed, people care about it so little that Linux is happily adding new syscalls that write data into variable-length buffers and expect the user to arbitrarily cast them back into structs.)
[0] https://en.cppreference.com/w/cpp/utility/launder#Notes https://en.cppreference.com/w/cpp/utility/launder#Notes
[1] https://stackoverflow.com/a/39382728 https://stackoverflow.com/a/39382728
- protomolecule 3y agoYou are right, thank you for taking time to explain.