From c837ff01ddbc4c24aa4eb091be3043d320f6d8cb Mon Sep 17 00:00:00 2001 From: Sy Brand Date: Mon, 1 Sep 2025 10:17:39 +0100 Subject: [PATCH] Don't copy unexpected object on throwing with exceptions disabled --- CMakeLists.txt | 2 +- include/tl/expected.hpp | 25 ++++++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b61c51f..8ad4e4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14) project(tl-expected HOMEPAGE_URL https://tl.tartanllama.xyz DESCRIPTION "C++11/14/17 std::expected with functional-style extensions" - VERSION 1.2.0 + VERSION 1.3.0 LANGUAGES CXX) include(CMakePackageConfigHelpers) diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index 31acb81..e99ea14 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -17,7 +17,7 @@ #define TL_EXPECTED_HPP #define TL_EXPECTED_VERSION_MAJOR 1 -#define TL_EXPECTED_VERSION_MINOR 2 +#define TL_EXPECTED_VERSION_MINOR 3 #define TL_EXPECTED_VERSION_PATCH 0 #include @@ -219,20 +219,11 @@ struct unexpect_t { }; static constexpr unexpect_t unexpect{}; -namespace detail { -template -[[noreturn]] TL_EXPECTED_11_CONSTEXPR void throw_exception(E &&e) { #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED - throw std::forward(e); -#else - (void)e; -#ifdef _MSC_VER - __assume(0); -#else - __builtin_unreachable(); +#define TL_EXPECTED_THROW_EXCEPTION(e) throw((e)); +#elif defined(_MSC_VER) +#define TL_EXPECTED_THROW_EXCEPTION(e) std::terminate(); #endif -#endif -} #ifndef TL_TRAITS_MUTEX #define TL_TRAITS_MUTEX @@ -2024,28 +2015,28 @@ public: detail::enable_if_t::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR const U &value() const & { if (!has_value()) - detail::throw_exception(bad_expected_access(err().value())); + TL_EXPECTED_THROW_EXCEPTION(bad_expected_access(err().value())); return val(); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &value() & { if (!has_value()) - detail::throw_exception(bad_expected_access(err().value())); + TL_EXPECTED_THROW_EXCEPTION(bad_expected_access(err().value())); return val(); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR const U &&value() const && { if (!has_value()) - detail::throw_exception(bad_expected_access(std::move(err()).value())); + TL_EXPECTED_THROW_EXCEPTION(bad_expected_access(std::move(err()).value())); return std::move(val()); } template ::value> * = nullptr> TL_EXPECTED_11_CONSTEXPR U &&value() && { if (!has_value()) - detail::throw_exception(bad_expected_access(std::move(err()).value())); + TL_EXPECTED_THROW_EXCEPTION(bad_expected_access(std::move(err()).value())); return std::move(val()); }