C++ code below demonstrates how GCC 13 optimizes std::memcpy and a temporary std::array:
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <new>
#include <type_traits>
template <class T>
T* startLifetimeAs(void* const buffer)
{
std::array<std::byte, sizeof(T)> representation;
// Preserve the bytes before placement new ends the previous lifetime.
std::memcpy(representation.data(), buffer, representation.size());
// Start the lifetime of T in the original storage.
T* const result = ::new (buffer) T;
// Initialize the object representation of the newly created T.
std::memcpy(result, representation.data(), representation.size());
return result;
}



