diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index c1e3c6eaf2..6d6a31749d 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -2235,11 +2235,12 @@ template struct PlacementStorage { /// @brief Raw byte storage, strictly aligned for type T. alignas(T) unsigned char data[sizeof(T)]; - /// @brief Retrieves a pointer to the constructed object. - T *get() { return std::launder(reinterpret_cast(data)); } + /// @brief Retrieves a pointer to the storage as the target type. + /// The caller must ensure the object has been constructed via placement new before dereferencing. + T *get() { return reinterpret_cast(data); } - /// @brief Retrieves a const pointer to the constructed object. - const T *get() const { return std::launder(reinterpret_cast(data)); } + /// @brief Retrieves a const pointer to the storage as the target type. + const T *get() const { return reinterpret_cast(data); } }; /// @name Internal functions diff --git a/tests/component_tests/mipi_dsi/test_mipi_dsi_config.py b/tests/component_tests/mipi_dsi/test_mipi_dsi_config.py index 519e39a1d9..eba6305a48 100644 --- a/tests/component_tests/mipi_dsi/test_mipi_dsi_config.py +++ b/tests/component_tests/mipi_dsi/test_mipi_dsi_config.py @@ -118,6 +118,10 @@ def test_code_generation( """Test code generation for display.""" main_cpp = generate_main(component_fixture_path("mipi_dsi.yaml")) + assert ( + "static esphome::PlacementStorage p4_nano_storage_;" + in main_cpp + ) assert ( "static mipi_dsi::MIPI_DSI *const p4_nano = p4_nano_storage_.get();" in main_cpp )