diff --git a/include/tl/optional.hpp b/include/tl/optional.hpp index e9c59c2..3364b70 100644 --- a/include/tl/optional.hpp +++ b/include/tl/optional.hpp @@ -27,6 +27,12 @@ #include #include +#ifdef __EXCEPTIONS +#define TL_OPTIONAL_THROW(e) throw e +#else +#define TL_OPTIONAL_THROW(e) std::abort() +#endif + #if (defined(_MSC_VER) && _MSC_VER == 1900) #define TL_OPTIONAL_MSVC2015 #endif @@ -73,7 +79,7 @@ namespace tl { template struct is_trivially_copy_constructible> : std::is_trivially_copy_constructible{}; -#endif +#endif } } #endif @@ -153,25 +159,25 @@ struct is_pointer_to_non_const_member_func : std::true_typ template struct is_pointer_to_non_const_member_func : std::true_type{}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func : std::true_type{}; template struct is_pointer_to_non_const_member_func : std::true_type{}; template struct is_pointer_to_non_const_member_func : std::true_type{}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func : std::true_type{}; template struct is_const_or_const_ref : std::false_type{}; template struct is_const_or_const_ref : std::true_type{}; -template struct is_const_or_const_ref : std::true_type{}; +template struct is_const_or_const_ref : std::true_type{}; #endif // std::invoke from C++17 // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround template ::value - && is_const_or_const_ref::value)>, + typename = enable_if_t::value + && is_const_or_const_ref::value)>, #endif typename = enable_if_t>::value>, int = 0> @@ -1229,7 +1235,7 @@ class optional : private detail::optional_move_assign_base, emplace(std::initializer_list il, Args &&... args) { *this = nullopt; this->construct(il, std::forward(args)...); - return value(); + return value(); } /// Swaps this optional with the other. @@ -1289,24 +1295,24 @@ class optional : private detail::optional_move_assign_base, TL_OPTIONAL_11_CONSTEXPR T &value() & { if (has_value()) return this->m_value; - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } TL_OPTIONAL_11_CONSTEXPR const T &value() const & { if (has_value()) return this->m_value; - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } TL_OPTIONAL_11_CONSTEXPR T &&value() && { if (has_value()) return std::move(this->m_value); - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } #ifndef TL_OPTIONAL_NO_CONSTRR TL_OPTIONAL_11_CONSTEXPR const T &&value() const && { if (has_value()) return std::move(this->m_value); - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } #endif @@ -2012,12 +2018,12 @@ template class optional { TL_OPTIONAL_11_CONSTEXPR T &value() { if (has_value()) return *m_value; - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } TL_OPTIONAL_11_CONSTEXPR const T &value() const { if (has_value()) return *m_value; - throw bad_optional_access(); + TL_OPTIONAL_THROW(bad_optional_access); } /// Returns the stored value if there is one, otherwise returns `u`