Skip to content

Commit 74ac0d9

Browse files
committed
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 e7b8c4f.
1 parent e7b8c4f commit 74ac0d9

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

include/pybind11/numpy.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <functional>
2222
#include <numeric>
2323
#include <sstream>
24+
#include <stdalign.h>
2425
#include <string>
2526
#include <type_traits>
2627
#include <typeindex>
@@ -54,21 +55,16 @@ class LazyInitializeAtLeastOnceDestroyNever {
5455
// Multiple threads may run this concurrently, but that is fine.
5556
auto value = initialize(); // May release and re-acquire the GIL.
5657
if (!initialized_) { // This runs with the GIL held,
57-
new (&value_) // therefore this is reached only once.
58-
T(std::move(value));
58+
new // therefore this is reached only once.
59+
(reinterpret_cast<T *>(value_storage_)) T(std::move(value));
5960
initialized_ = true;
6061
}
6162
}
62-
return value_;
63+
return *reinterpret_cast<T *>(value_storage_);
6364
}
6465

65-
LazyInitializeAtLeastOnceDestroyNever() {}
66-
~LazyInitializeAtLeastOnceDestroyNever() {}
67-
6866
private:
69-
union {
70-
T value_;
71-
};
67+
alignas(T) char value_storage_[sizeof(T)];
7268
bool initialized_ = false;
7369
};
7470

0 commit comments

Comments
 (0)