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

Commit b269fa6

Browse files
committed
rename Result private member variables
1 parent 90870da commit b269fa6

1 file changed

Lines changed: 36 additions & 30 deletions

File tree

include/common/result.hpp

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class Result {
101101
template<typename U>
102102
requires std::constructible_from<T, U>
103103
constexpr Result(U&& value)
104-
: error(std::monostate()),
105-
value(std::forward<U>(value)) {}
104+
: m_error(std::monostate()),
105+
m_value(std::forward<U>(value)) {}
106106

107107
/**
108108
* @brief Construct a Result with a value and an error.
@@ -115,8 +115,8 @@ class Result {
115115
requires std::constructible_from<T, U>
116116
&& (std::same_as<std::remove_cvref_t<E>, Errs> || ...)
117117
constexpr Result(U&& value, E&& error)
118-
: value(std::forward<U>(value)),
119-
error(std::forward<E>(error)) {}
118+
: m_value(std::forward<U>(value)),
119+
m_error(std::forward<E>(error)) {}
120120

121121
/**
122122
* @brief Construct a Result with an error, initializing the value to its sentinel.
@@ -127,8 +127,8 @@ class Result {
127127
template<typename E>
128128
requires Sentinel<T> && (std::same_as<std::remove_cvref_t<E>, Errs> || ...)
129129
constexpr Result(E&& error)
130-
: error(std::forward<E>(error)),
131-
value(sentinel_v<T>) {}
130+
: m_error(std::forward<E>(error)),
131+
m_value(sentinel_v<T>) {}
132132

133133
/**
134134
* @brief Get an error of type E if present (const-qualified overload).
@@ -138,8 +138,8 @@ class Result {
138138
template<typename E>
139139
requires(std::same_as<E, Errs> || ...)
140140
constexpr std::optional<E> get() const& {
141-
if (std::holds_alternative<E>(error)) {
142-
return std::get<E>(error);
141+
if (std::holds_alternative<E>(m_error)) {
142+
return std::get<E>(m_error);
143143
} else {
144144
return std::nullopt;
145145
}
@@ -153,8 +153,8 @@ class Result {
153153
template<typename E>
154154
requires(std::same_as<E, Errs> || ...)
155155
constexpr std::optional<E> get() && {
156-
if (std::holds_alternative<E>(error)) {
157-
return std::move(std::get<E>(error));
156+
if (std::holds_alternative<E>(m_error)) {
157+
return std::move(std::get<E>(m_error));
158158
} else {
159159
return std::nullopt;
160160
}
@@ -168,8 +168,8 @@ class Result {
168168
template<typename E>
169169
requires(std::same_as<E, Errs> || ...)
170170
constexpr const std::optional<E> get() const&& {
171-
if (std::holds_alternative<E>(error)) {
172-
return std::move(std::get<E>(error));
171+
if (std::holds_alternative<E>(m_error)) {
172+
return std::move(std::get<E>(m_error));
173173
} else {
174174
return std::nullopt;
175175
}
@@ -182,7 +182,7 @@ class Result {
182182
template<typename U = T>
183183
requires std::same_as<U, T>
184184
constexpr T get() const& {
185-
return value;
185+
return m_value;
186186
}
187187

188188
/**
@@ -192,29 +192,33 @@ class Result {
192192
template<typename U = T>
193193
requires std::same_as<U, T>
194194
constexpr T get() && {
195-
return std::move(value);
195+
return std::move(m_value);
196+
}
197+
198+
constexpr bool has_error() {
199+
return !std::holds_alternative<std::monostate>(m_value);
196200
}
197201

198202
constexpr operator T&() & {
199-
return value;
203+
return m_value;
200204
}
201205

202206
constexpr operator const T&() const& {
203-
return value;
207+
return m_value;
204208
};
205209

206210
constexpr operator T&&() && {
207-
return std::move(value);
211+
return std::move(m_value);
208212
}
209213

210214
constexpr operator const T&&() const&& {
211-
return std::move(value);
215+
return std::move(m_value);
212216
}
213217

214218
private:
215219
// instead of wrapping the variant in std::optional, we can use std::monostate
216-
std::variant<std::monostate, Errs...> error;
217-
T value;
220+
std::variant<std::monostate, Errs...> m_error;
221+
T m_value;
218222
};
219223

220224
/**
@@ -233,7 +237,7 @@ template<typename LhsT, typename RhsT, typename... LhsErrs, typename... RhsErrs>
233237
requires std::equality_comparable_with<LhsT, RhsT>
234238
constexpr bool
235239
operator==(const Result<LhsT, LhsErrs...>& lhs, const Result<RhsT, RhsErrs...>& rhs) {
236-
return lhs.value == rhs.value;
240+
return lhs.get() == rhs.get();
237241
}
238242

239243
/**
@@ -252,13 +256,13 @@ class Result<void, Errs...> {
252256
template<typename E>
253257
requires(std::same_as<std::remove_cvref_t<E>, Errs> || ...)
254258
constexpr Result(E&& error)
255-
: error(std::forward<E>(error)) {}
259+
: m_error(std::forward<E>(error)) {}
256260

257261
/**
258262
* @brief Construct a Result with no error (success state).
259263
*/
260264
constexpr Result()
261-
: error(std::monostate()) {}
265+
: m_error(std::monostate()) {}
262266

263267
/**
264268
* @brief Get an error of type E if present (const-qualified overload).
@@ -268,8 +272,8 @@ class Result<void, Errs...> {
268272
template<typename E>
269273
requires(std::same_as<E, Errs> || ...)
270274
constexpr std::optional<E> get() const& {
271-
if (std::holds_alternative<E>(error)) {
272-
return std::get<E>(error);
275+
if (std::holds_alternative<E>(m_error)) {
276+
return std::get<E>(m_error);
273277
} else {
274278
return std::nullopt;
275279
}
@@ -283,8 +287,8 @@ class Result<void, Errs...> {
283287
template<typename E>
284288
requires(std::same_as<E, Errs> || ...)
285289
constexpr std::optional<E> get() && {
286-
if (std::holds_alternative<E>(error)) {
287-
return std::move(std::get<E>(error));
290+
if (std::holds_alternative<E>(m_error)) {
291+
return std::move(std::get<E>(m_error));
288292
} else {
289293
return std::nullopt;
290294
}
@@ -298,14 +302,16 @@ class Result<void, Errs...> {
298302
template<typename E>
299303
requires(std::same_as<E, Errs> || ...)
300304
constexpr const std::optional<E> get() const&& {
301-
if (std::holds_alternative<E>(error)) {
302-
return std::move(std::get<E>(error));
305+
if (std::holds_alternative<E>(m_error)) {
306+
return std::move(std::get<E>(m_error));
303307
} else {
304308
return std::nullopt;
305309
}
306310
}
307311

308-
std::variant<std::monostate, Errs...> error; ///< Variant holding an error or monostate.
312+
private:
313+
// instead of wrapping the variant in std::optional, we can use std::monostate
314+
std::variant<std::monostate, Errs...> m_error; ///< Variant holding an error or monostate.
309315
};
310316

311317
} // namespace zest

0 commit comments

Comments
 (0)