From 74ac0d99fb5c70aa66248fed5d7536089da3e253 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 9 Oct 2023 05:08:24 -0700 Subject: [PATCH] Revert "Go back to using `union` as originally suggested by jbms@. The trick (also suggested by jbms@) is to add empty ctor + dtor." This reverts commit e7b8c4f0fcd72191e88d1c17abf5da08fe3a9c6f. --- include/pybind11/numpy.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 0a00120c9..5a60f18b2 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -54,21 +55,16 @@ public: // Multiple threads may run this concurrently, but that is fine. auto value = initialize(); // May release and re-acquire the GIL. if (!initialized_) { // This runs with the GIL held, - new (&value_) // therefore this is reached only once. - T(std::move(value)); + new // therefore this is reached only once. + (reinterpret_cast(value_storage_)) T(std::move(value)); initialized_ = true; } } - return value_; + return *reinterpret_cast(value_storage_); } - LazyInitializeAtLeastOnceDestroyNever() {} - ~LazyInitializeAtLeastOnceDestroyNever() {} - private: - union { - T value_; - }; + alignas(T) char value_storage_[sizeof(T)]; bool initialized_ = false; };