Skip to content

Commit e7b8c4f

Browse files
committed
Go back to using union as originally suggested by jbms@. The trick (also suggested by jbms@) is to add empty ctor + dtor.
1 parent 38317c3 commit e7b8c4f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

include/pybind11/numpy.h

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

65+
LazyInitializeAtLeastOnceDestroyNever() {}
66+
~LazyInitializeAtLeastOnceDestroyNever() {}
67+
6668
private:
67-
alignas(T) char value_storage_[sizeof(T)];
69+
union {
70+
T value_;
71+
};
6872
bool initialized_ = false;
6973
};
7074

0 commit comments

Comments
 (0)