File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments