c++c++20stdtype-punningbit-cast

Any useful difference between std::bit_cast and std::start_lifetime_as?


std::bit_cast is apparently being introduced in c++20. and std::start_lifetime_as is proposed for c++23 (from P0593R5). As they appear to both require that the datatypes involved be trivial anyways, will there be any need for the former once the latter is introduced?


Solution

  • The answer is trivial: bit_cast returns a value, whereas start_lifetime_as “alters” memory (in a way that exists in the abstract machine but is not expected to affect any physical bits). You use the former to (once) interpret an existing object as a set of bits; you use the latter to (permanently) interpret existing bits as an object.