| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -373,21 +373,20 @@ using in = | |||
| 373 | 373 | template<typename T> | |
| 374 | 374 | class deferred_init { | |
| 375 | 375 | bool init = false; | |
| 376 | - union { | ||
| 377 | - int i; | ||
| 378 | - T t; | ||
| 379 | - }; | ||
| 376 | + std::aligned_storage<sizeof(T), alignof(T)> data; | ||
| 377 | + | ||
| 378 | + auto t() -> T& { return *std::launder(reinterpret_cast<T*>(&data)); } | ||
| 380 | 379 | ||
| 381 | 380 | template<typename U> | |
| 382 | 381 | friend class out; | |
| 383 | 382 | ||
| 384 | 383 | public: | |
| 385 | - deferred_init() noexcept : i {0} { } | ||
| 386 | - ~deferred_init() noexcept { if (init) t.~T(); } | ||
| 387 | - auto value() noexcept -> T& { Default.expects(init); return t; } | ||
| 384 | + deferred_init() noexcept { } | ||
| 385 | + ~deferred_init() noexcept { if (init) t().~T(); } | ||
| 386 | + auto value() noexcept -> T& { Default.expects(init); return t(); } | ||
| 388 | 387 | ||
| 389 | - auto construct (auto ...args) -> void { Default.expects(!init); new (&t) T(std::forward<decltype(args)>(args)...); init = true; } | ||
| 390 | - auto construct_list(auto ...args) -> void { Default.expects(!init); new (&t) T{std::forward<decltype(args)>(args)...}; init = true; } | ||
| 388 | + auto construct (auto&& ...args) -> void { Default.expects(!init); new (&data) T(std::forward<decltype(args)>(args)...); init = true; } | ||
| 389 | + auto construct_list(auto&& ...args) -> void { Default.expects(!init); new (&data) T{std::forward<decltype(args)>(args)...}; init = true; } | ||
| 391 | 390 | }; | |
| 392 | 391 | ||
| 393 | 392 | template<typename T> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments