utils: add result_with_exception_ptr

Add `result_with_exception_ptr` result type.
Successful result has user specified type.
Failed result has std::exception_ptr.

This approach is simpler than `result_with_exception`. It does not require
user to pass exception types as variadic template through all the callstack.
Specific exception type can still be accessed without costly std::rethrow_exception(eptr)
by using `try_catch`, if configured so via `USE_OPTIMIZED_EXCEPTION_HANDLING`.
This means no information loss, but less verbosity when writing result types.

Refs: #24567
This commit is contained in:
Dario Mirovic
2025-08-08 08:39:31 +02:00
parent 8b0a551177
commit f01efd822e

View File

@@ -59,4 +59,24 @@ concept ResultRebindableTo =
template<typename T, ExceptionContainerResult R>
using rebind_result = bo::result<T, typename R::error_type, exception_container_throw_policy>;
struct result_with_exception_ptr_throw_policy : bo::policy::base {
template<class Impl> static constexpr void wide_value_check(Impl&& self) {
if (!base::_has_value(self)) {
std::rethrow_exception(base::_error(self));
}
}
template<class Impl> static constexpr void wide_error_check(Impl&& self) {
if (!base::_has_error(self)) {
throw bo::bad_result_access("no error");
}
}
};
template<typename T>
using result_with_exception_ptr = bo::result<T, std::exception_ptr, result_with_exception_ptr_throw_policy>;
template<typename R>
concept ExceptionPtrResult = bo::is_basic_result<R>::value && std::same_as<typename R::error_type, std::exception_ptr>;
}