File tree 1 file changed +5
-9
lines changed
1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change 21
21
#include < functional>
22
22
#include < numeric>
23
23
#include < sstream>
24
+ #include < stdalign.h>
24
25
#include < string>
25
26
#include < type_traits>
26
27
#include < typeindex>
@@ -54,21 +55,16 @@ class LazyInitializeAtLeastOnceDestroyNever {
54
55
// Multiple threads may run this concurrently, but that is fine.
55
56
auto value = initialize (); // May release and re-acquire the GIL.
56
57
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));
59
60
initialized_ = true ;
60
61
}
61
62
}
62
- return value_ ;
63
+ return * reinterpret_cast <T *>(value_storage_) ;
63
64
}
64
65
65
- LazyInitializeAtLeastOnceDestroyNever () {}
66
- ~LazyInitializeAtLeastOnceDestroyNever () {}
67
-
68
66
private:
69
- union {
70
- T value_;
71
- };
67
+ alignas (T) char value_storage_[sizeof (T)];
72
68
bool initialized_ = false ;
73
69
};
74
70
You can’t perform that action at this time.
0 commit comments