Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Commit b95b00a

Browse files
committed
simplify Result::get
1 parent d8837c3 commit b95b00a

1 file changed

Lines changed: 17 additions & 97 deletions

File tree

include/common/result.hpp

Lines changed: 17 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -206,69 +206,19 @@ class Result {
206206
: m_error(std::forward<E>(error)),
207207
m_value(traits::sentinel_v<T>) {}
208208

209-
/**
210-
* @brief Get an error of type E if present (const-qualified overload).
211-
* @tparam E Error type to retrieve, must be in Errs and derived from ResultError.
212-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
213-
*/
214-
template<traits::IsResultError E>
215-
requires traits::is_in_pack_v<E, Errs...>
216-
constexpr std::optional<E> get() const& {
217-
if (std::holds_alternative<E>(m_error)) {
218-
return std::get<E>(m_error);
219-
} else {
220-
return std::nullopt;
221-
}
222-
}
223-
224-
/**
225-
* @brief Get an error of type E if present (rvalue overload).
226-
* @tparam E Error type to retrieve.
227-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
228-
*/
229-
template<traits::IsResultError E>
209+
template<typename Self, traits::IsResultError E>
230210
requires traits::is_in_pack_v<E, Errs...>
231-
constexpr std::optional<E> get() && {
232-
if (std::holds_alternative<E>(m_error)) {
233-
return std::move(std::get<E>(m_error));
211+
constexpr auto&& get_error(this Self&& self) {
212+
if (std::holds_alternative<E>(self.m_error)) {
213+
return std::optional(std::holds_alternative<E>(std::forward<Self>(self).m_error));
234214
} else {
235-
return std::nullopt;
215+
return std::optional<E>();
236216
}
237217
}
238218

239-
/**
240-
* @brief Get an error of type E if present (const rvalue overload).
241-
* @tparam E Error type to retrieve.
242-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
243-
*/
244-
template<traits::IsResultError E>
245-
requires traits::is_in_pack_v<E, Errs...>
246-
constexpr const std::optional<E> get() const&& {
247-
if (std::holds_alternative<E>(m_error)) {
248-
return std::move(std::get<E>(m_error));
249-
} else {
250-
return std::nullopt;
251-
}
252-
}
253-
254-
/**
255-
* @brief Get the stored value (const-qualified overload).
256-
* @return T Copy of the stored value.
257-
*/
258-
template<typename U = T>
259-
requires std::same_as<U, T>
260-
constexpr T get() const& {
261-
return m_value;
262-
}
263-
264-
/**
265-
* @brief Get the stored value (rvalue overload).
266-
* @return T Moved value.
267-
*/
268-
template<typename U = T>
269-
requires std::same_as<U, T>
270-
constexpr T get() && {
271-
return std::move(m_value);
219+
template<typename Self>
220+
constexpr auto&& get_value(this Self&& self) {
221+
return std::forward<Self>(self).m_value;
272222
}
273223

274224
constexpr bool has_error() {
@@ -313,7 +263,7 @@ template<typename LhsT, typename RhsT, typename... LhsErrs, typename... RhsErrs>
313263
requires std::equality_comparable_with<LhsT, RhsT>
314264
constexpr bool
315265
operator==(const Result<LhsT, LhsErrs...>& lhs, const Result<RhsT, RhsErrs...>& rhs) {
316-
return lhs.get() == rhs.get();
266+
return lhs.get_value() == rhs.get_value();
317267
}
318268

319269
/**
@@ -340,49 +290,19 @@ class Result<void, Errs...> {
340290
constexpr Result()
341291
: m_error(std::monostate()) {}
342292

343-
/**
344-
* @brief Get an error of type E if present (const-qualified overload).
345-
* @tparam E Error type to retrieve.
346-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
347-
*/
348-
template<traits::IsResultError E>
349-
requires traits::is_in_pack_v<E, Errs...>
350-
constexpr std::optional<E> get() const& {
351-
if (std::holds_alternative<E>(m_error)) {
352-
return std::get<E>(m_error);
353-
} else {
354-
return std::nullopt;
355-
}
356-
}
357-
358-
/**
359-
* @brief Get an error of type E if present (rvalue overload).
360-
* @tparam E Error type to retrieve.
361-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
362-
*/
363-
template<traits::IsResultError E>
293+
template<typename Self, traits::IsResultError E>
364294
requires traits::is_in_pack_v<E, Errs...>
365-
constexpr std::optional<E> get() && {
366-
if (std::holds_alternative<E>(m_error)) {
367-
return std::move(std::get<E>(m_error));
295+
constexpr auto&& get_error(this Self&& self) {
296+
if (std::holds_alternative<E>(self.m_error)) {
297+
return std::optional(std::holds_alternative<E>(std::forward<Self>(self).m_error));
368298
} else {
369-
return std::nullopt;
299+
return std::optional<E>();
370300
}
371301
}
372302

373-
/**
374-
* @brief Get an error of type E if present (const rvalue overload).
375-
* @tparam E Error type to retrieve.
376-
* @return std::optional<E> Contains the error if present; otherwise nullopt.
377-
*/
378-
template<traits::IsResultError E>
379-
requires traits::is_in_pack_v<E, Errs...>
380-
constexpr const std::optional<E> get() const&& {
381-
if (std::holds_alternative<E>(m_error)) {
382-
return std::move(std::get<E>(m_error));
383-
} else {
384-
return std::nullopt;
385-
}
303+
template<typename Self>
304+
constexpr auto&& get_value(this Self&& self) {
305+
return std::forward<Self>(self).m_value;
386306
}
387307

388308
constexpr bool has_error() {

0 commit comments

Comments
 (0)