Skip to content

Commit fcfc7f5

Browse files
committed
Allow forwarding arguments to constructor for unexpected creation
1 parent c983072 commit fcfc7f5

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

lib/public/StormByte/expected.hxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,27 @@ namespace StormByte {
106106
std::make_shared<E>(std::move(formatted_message))
107107
);
108108
}
109+
110+
/**
111+
* @brief Construct an `std::unexpected` by forwarding constructor args into `E`.
112+
*
113+
* This overload allows calls like `Unexpected<MyError>(arg1, arg2)` and will
114+
* construct a `MyError` in-place using the forwarded arguments and store it
115+
* inside a `std::shared_ptr` returned inside `std::unexpected`.
116+
*
117+
* The overload is SFINAE-constrained to avoid ambiguity with the existing
118+
* `Unexpected(E&&)` overload when a single argument would directly match
119+
* that overload.
120+
*/
121+
122+
template <typename E, typename... Args>
123+
auto Unexpected(Args&&... args)
124+
-> std::enable_if_t<
125+
!(sizeof...(Args) == 1 && std::is_constructible_v<std::decay_t<E>, Args&&...>),
126+
std::unexpected<std::shared_ptr<std::decay_t<E>>>
127+
> {
128+
return std::unexpected<std::shared_ptr<std::decay_t<E>>>(
129+
std::make_shared<std::decay_t<E>>(new std::decay_t<E>(std::forward<Args>(args)...))
130+
);
131+
}
109132
}

0 commit comments

Comments
 (0)