Skip to content

Commit 6ea1e39

Browse files
committed
improve Result method qualifications
1 parent d6971b2 commit 6ea1e39

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

include/common/result.hpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ class Result {
5151
* Constraints:
5252
* - T must be default initializable
5353
*/
54-
constexpr Result(E error)
54+
template<typename F>
55+
constexpr Result(F&& error)
5556
requires std::default_initializable<T>
56-
: error(error) {}
57+
: error(std::forward<F>(error)) {}
5758

5859
/**
5960
* @brief Construct with an "error" value and "normal" value
@@ -64,12 +65,13 @@ class Result {
6465
* @param value argument to initialize the "normal" value with
6566
*
6667
* Constraints:
67-
* - T must be constructible with perfectly forwarded value argument
68+
* - T must be constructible given perfectly forwarded value argument
69+
* - U must be constructible given perfectly forwarded error argument
6870
*/
69-
template<typename U>
70-
requires std::constructible_from<T, U&&>
71-
constexpr Result(E error, U&& value)
72-
: error(error),
71+
template<typename F, typename U>
72+
requires std::constructible_from<T, U&&> && std::constructible_from<E, F&&>
73+
constexpr Result(F&& error, U&& value)
74+
: error(std::forward<F>(error)),
7375
value(std::forward<U>(value)) {}
7476

7577
/**
@@ -102,7 +104,7 @@ class Result {
102104
/**
103105
* @brief Get the value object
104106
*
105-
* @tparam Self
107+
* @tparam Self deduced self type
106108
*
107109
* @param self
108110
*
@@ -119,16 +121,18 @@ class Result {
119121
* @return true an error is contained
120122
* @return false an error is not contained
121123
*/
122-
constexpr bool has_error() {
124+
constexpr bool has_error() const {
123125
return error.has_value();
124126
}
125127

126128
/**
127129
* @brief Get the error
128130
*
129-
* @tparam Self
131+
* @tparam Self deduced self type
132+
*
130133
* @param self
131-
* @return constexpr auto
134+
*
135+
* @return std::optional<E>
132136
*/
133137
template<typename Self>
134138
constexpr auto get_error(this Self&& self) {
@@ -157,8 +161,13 @@ class Result<void, E> {
157161
* @brief Construct with an "error" value, and the default "normal" value
158162
*
159163
* @param error argument to initialize the "error" value with
164+
*
165+
* Constraints:
166+
* E must be constructible given perfectly forwarded error argument
160167
*/
161-
constexpr Result(E error)
168+
template<typename F>
169+
requires std::constructible_from<E, F&&>
170+
constexpr Result(F&& error)
162171
: error(error) {}
163172

164173
/**
@@ -167,16 +176,18 @@ class Result<void, E> {
167176
* @return true an error is contained
168177
* @return false an error is not contained
169178
*/
170-
constexpr bool has_error() {
179+
constexpr bool has_error() const {
171180
return error.has_value();
172181
}
173182

174183
/**
175-
* @brief Get the error
184+
* @brief Get the error, wrapped in std::optional
185+
*
186+
* @tparam Self deduced self type
176187
*
177-
* @tparam Self
178188
* @param self
179-
* @return constexpr auto
189+
*
190+
* @return std::optional<E>
180191
*/
181192
template<typename Self>
182193
constexpr auto get_error(this Self&& self) {

0 commit comments

Comments
 (0)