From 2d3b501fd846254fe96754727a9b0e848f7c2626 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 9 Oct 2021 05:30:16 -0700 Subject: Add more noexcept when possible And try to fix MSVC missing a constructor --- common/aloptional.h | 58 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'common') diff --git a/common/aloptional.h b/common/aloptional.h index 55c1726b..609c10c7 100644 --- a/common/aloptional.h +++ b/common/aloptional.h @@ -15,6 +15,7 @@ struct in_place_t { }; constexpr nullopt_t nullopt{}; constexpr in_place_t in_place{}; +#define NOEXCEPT_AS(...) noexcept(noexcept(__VA_ARGS__)) /* Base storage struct for an optional. Defines a trivial destructor, for types * that can be trivially destructed. @@ -27,9 +28,11 @@ struct optstore_base { T mValue; }; - optstore_base() { } + optstore_base() noexcept { } + optstore_base(nullopt_t) noexcept { } template explicit optstore_base(in_place_t, Args&& ...args) + noexcept(std::is_nothrow_constructible::value) : mHasValue{true}, mValue{std::forward(args)...} { } ~optstore_base() = default; @@ -44,9 +47,11 @@ struct optstore_base { T mValue; }; - optstore_base() { } + optstore_base() noexcept { } + optstore_base(nullopt_t) noexcept { } template explicit optstore_base(in_place_t, Args&& ...args) + noexcept(std::is_nothrow_constructible::value) : mHasValue{true}, mValue{std::forward(args)...} { } ~optstore_base() { if(mHasValue) al::destroy_at(std::addressof(mValue)); } @@ -60,7 +65,7 @@ struct optstore_helper : public optstore_base { using optstore_base::optstore_base; template - void construct(Args&& ...args) noexcept(noexcept(T{std::forward(args)...})) + void construct(Args&& ...args) noexcept(std::is_nothrow_constructible::value) { al::construct_at(std::addressof(this->mValue), std::forward(args)...); this->mHasValue = true; @@ -74,6 +79,8 @@ struct optstore_helper : public optstore_base { } void assign(const optstore_helper &rhs) + noexcept(std::is_nothrow_copy_constructible::value + && std::is_nothrow_copy_assignable::value) { if(!rhs.mHasValue) this->reset(); @@ -84,6 +91,8 @@ struct optstore_helper : public optstore_base { } void assign(optstore_helper&& rhs) + noexcept(std::is_nothrow_move_constructible::value + && std::is_nothrow_move_assignable::value) { if(!rhs.mHasValue) this->reset(); @@ -105,9 +114,9 @@ template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; optional_storage(const optional_storage&) = default; - optional_storage( optional_storage&&) = default; + optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage&) = default; - optional_storage& operator=( optional_storage&&) = default; + optional_storage& operator=(optional_storage&&) = default; }; /* Non-trivial move assignment. */ @@ -117,7 +126,7 @@ struct optional_storage : optstore_helper { optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; optional_storage& operator=(const optional_storage&) = default; - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -126,10 +135,10 @@ template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; optional_storage(const optional_storage&) = default; - optional_storage(optional_storage&& rhs) + optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } optional_storage& operator=(const optional_storage&) = default; - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -139,7 +148,7 @@ struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&&) = default; }; @@ -148,10 +157,10 @@ struct optional_storage : optstore_helper { template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; - optional_storage(const optional_storage &rhs) + optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } optional_storage(optional_storage&&) = default; - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } optional_storage& operator=(optional_storage&&) = default; }; @@ -162,9 +171,9 @@ struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; optional_storage(const optional_storage&) = default; optional_storage(optional_storage&&) = default; - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -173,11 +182,11 @@ template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; optional_storage(const optional_storage&) = default; - optional_storage(optional_storage&& rhs) + optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -185,12 +194,12 @@ struct optional_storage : optstore_helper { template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; - optional_storage(const optional_storage &rhs) + optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } optional_storage(optional_storage&&) = default; - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -198,13 +207,13 @@ struct optional_storage : optstore_helper { template struct optional_storage : optstore_helper { using optstore_helper::optstore_helper; - optional_storage(const optional_storage &rhs) + optional_storage(const optional_storage &rhs) NOEXCEPT_AS(this->construct(rhs.mValue)) { if(rhs.mHasValue) this->construct(rhs.mValue); } - optional_storage(optional_storage&& rhs) + optional_storage(optional_storage&& rhs) NOEXCEPT_AS(this->construct(std::move(rhs.mValue))) { if(rhs.mHasValue) this->construct(std::move(rhs.mValue)); } - optional_storage& operator=(const optional_storage &rhs) + optional_storage& operator=(const optional_storage &rhs) NOEXCEPT_AS(this->assign(rhs)) { this->assign(rhs); return *this; } - optional_storage& operator=(optional_storage&& rhs) + optional_storage& operator=(optional_storage&& rhs) NOEXCEPT_AS(this->assign(std::move(rhs))) { this->assign(std::move(rhs)); return *this; } }; @@ -221,7 +230,7 @@ public: optional() = default; optional(const optional&) = default; optional(optional&&) = default; - optional(nullopt_t) noexcept { } + optional(nullopt_t) noexcept : mStore{nullopt} { } template explicit optional(in_place_t, Args&& ...args) : mStore{al::in_place, std::forward(args)...} @@ -282,6 +291,7 @@ template inline optional make_optional(std::initializer_list il, Args&& ...args) { return optional{in_place, il, std::forward(args)...}; } +#undef NOEXCEPT_AS } // namespace al #endif /* AL_OPTIONAL_H */ -- cgit v1.2.3