diff --git a/ctl/shared_ptr.h b/ctl/shared_ptr.h index 7702a99b0..a678d50e3 100644 --- a/ctl/shared_ptr.h +++ b/ctl/shared_ptr.h @@ -73,15 +73,17 @@ struct shared_pointer : shared_control } }; -template +template struct shared_emplace : shared_control { - union { + union + { T t; }; - template - void construct(Args&&... args) { + template + void construct(Args&&... args) + { ::new (&t) T(ctl::forward(args)...); } @@ -90,7 +92,7 @@ struct shared_emplace : shared_control return new shared_emplace(); } -private: + private: explicit constexpr shared_emplace() noexcept { } @@ -123,12 +125,12 @@ class shared_ptr public: using element_type = T; // TODO(mrdomino): remove extent? - constexpr shared_ptr(nullptr_t = nullptr) noexcept - : p(nullptr), rc(nullptr) + constexpr shared_ptr(nullptr_t = nullptr) noexcept : p(nullptr), rc(nullptr) { } - explicit shared_ptr(auto* const p) : p(p), rc(__::shared_pointer::make(p)) + explicit shared_ptr(auto* const p) + : p(p), rc(__::shared_pointer::make(p)) { } @@ -144,14 +146,14 @@ class shared_ptr r.rc = nullptr; } - template + template shared_ptr(const shared_ptr& r, T* const p) noexcept : p(p), rc(r.rc) { if (rc) rc->keep_shared(); } - template + template shared_ptr(shared_ptr&& r, T* const p) noexcept : p(p), rc(r.rc) { r.p = nullptr; @@ -228,7 +230,7 @@ class shared_ptr return p; } - template + template bool owner_before(const shared_ptr& r) const noexcept { return p < r.p; @@ -242,15 +244,16 @@ class shared_ptr { } - template + template friend shared_ptr make_shared(Args&&... args); T* p; __::shared_control* rc; }; -template -shared_ptr make_shared(Args&&... args) +template +shared_ptr +make_shared(Args&&... args) { auto rc = __::shared_emplace::make_unique(); rc->construct(ctl::forward(args)...); diff --git a/test/ctl/shared_ptr_test.cc b/test/ctl/shared_ptr_test.cc index 2d1ecaec7..2e3b40ab8 100644 --- a/test/ctl/shared_ptr_test.cc +++ b/test/ctl/shared_ptr_test.cc @@ -26,8 +26,10 @@ template using Ptr = ctl::shared_ptr; -template -Ptr Mk(Args&&... args) { +template +Ptr +Mk(Args&&... args) +{ return ctl::make_shared(ctl::forward(args)...); }