header_utils
Loading...
Searching...
No Matches
expected.h
1
4
5#pragma once
6
7#define GHPL_LATEST_MSVC_VERSION_WITH_EXPECTED_BUGS 1939
8#if __has_include(<expected>) && (_MSC_VER > GHPL_LATEST_MSVC_VERSION_WITH_EXPECTED_BUGS)
9#include <expected>
10namespace ghassanpl
11{
12 using std::expected;
13 using std::unexpected;
14
16 static_assert(std::is_swappable_v<std::string>);
17 static_assert(std::is_swappable_v<std::error_code>);
18}
19#elif __has_include(<tl/expected.hpp>)
20#include <tl/expected.hpp>
21namespace ghassanpl
22{
23 using tl::expected;
24 using tl::unexpected;
25}
26#elif defined(GHPL_EXPECTED_HEADER)
27#include GHPL_EXPECTED_HEADER
28#else
29#error "No expected implementation found"
30#endif
31
32namespace ghassanpl
33{
34
37 template <typename FUNC, typename... ARGS>
38 inline auto call_with_expected_ec(FUNC&& func, ARGS&&... args) noexcept(noexcept(func(std::forward<ARGS>(args)...)))
39 -> expected<std::invoke_result_t<FUNC, ARGS&&...>, std::error_code>
40 {
41 std::error_code ec{};
42 if constexpr (std::is_void_v<std::invoke_result_t<FUNC, ARGS&&...>>)
43 {
44 func(std::forward<ARGS>(args)..., ec);
45 if (ec)
46 return unexpected(ec);
47 return {};
48 }
49 else
50 {
51 auto result = func(std::forward<ARGS>(args)..., ec);
52 if (ec)
53 return unexpected(ec);
54 return result;
55 }
56 }
57
58}
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
Primary namespace for everything in this library.
Definition align+rec2.h:10
auto call_with_expected_ec(FUNC &&func, ARGS &&... args) noexcept(noexcept(func(std::forward< ARGS >(args)...))) -> expected< std::invoke_result_t< FUNC, ARGS &&... >, std::error_code >
Calls the given function with args and an std::error_code as the last argument.
Definition expected.h:38