diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 0a00120c90..5a60f18b2a 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 @@ class LazyInitializeAtLeastOnceDestroyNever { // 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; };