mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-29 08:32:02 +00:00
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 e7b8c4f0fc
.
This commit is contained in:
parent
e7b8c4f0fc
commit
74ac0d99fb
@ -21,6 +21,7 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdalign.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <typeindex>
|
#include <typeindex>
|
||||||
@ -54,21 +55,16 @@ public:
|
|||||||
// Multiple threads may run this concurrently, but that is fine.
|
// Multiple threads may run this concurrently, but that is fine.
|
||||||
auto value = initialize(); // May release and re-acquire the GIL.
|
auto value = initialize(); // May release and re-acquire the GIL.
|
||||||
if (!initialized_) { // This runs with the GIL held,
|
if (!initialized_) { // This runs with the GIL held,
|
||||||
new (&value_) // therefore this is reached only once.
|
new // therefore this is reached only once.
|
||||||
T(std::move(value));
|
(reinterpret_cast<T *>(value_storage_)) T(std::move(value));
|
||||||
initialized_ = true;
|
initialized_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return value_;
|
return *reinterpret_cast<T *>(value_storage_);
|
||||||
}
|
}
|
||||||
|
|
||||||
LazyInitializeAtLeastOnceDestroyNever() {}
|
|
||||||
~LazyInitializeAtLeastOnceDestroyNever() {}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
union {
|
alignas(T) char value_storage_[sizeof(T)];
|
||||||
T value_;
|
|
||||||
};
|
|
||||||
bool initialized_ = false;
|
bool initialized_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user