Skip to content

Commit

Permalink
Revert "Go back to using union as originally suggested by jbms@. Th…
Browse files Browse the repository at this point in the history
…e trick (also suggested by jbms@) is to add empty ctor + dtor."

This reverts commit e7b8c4f.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Oct 9, 2023
1 parent e7b8c4f commit 74ac0d9
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <functional>
#include <numeric>
#include <sstream>
#include <stdalign.h>
#include <string>
#include <type_traits>
#include <typeindex>
Expand Down Expand Up @@ -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<T *>(value_storage_)) T(std::move(value));
initialized_ = true;
}
}
return value_;
return *reinterpret_cast<T *>(value_storage_);
}

LazyInitializeAtLeastOnceDestroyNever() {}
~LazyInitializeAtLeastOnceDestroyNever() {}

private:
union {
T value_;
};
alignas(T) char value_storage_[sizeof(T)];
bool initialized_ = false;
};

Expand Down

0 comments on commit 74ac0d9

Please sign in to comment.