Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5///
6/// @file result.hpp
7/// @brief Provides @c arene::base::result, a value/error sum-type suitable for use as a return value, similar to
8/// @c std::expected
9///
10#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RESULT_RESULT_HPP_
11#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RESULT_RESULT_HPP_
12
13// IWYU pragma: private, include "arene/base/result.hpp"
14
15// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
16
17// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
18
25#include "arene/base/result/detail/in_place_error.hpp" // IWYU pragma: export
26#include "arene/base/result/detail/in_place_value.hpp" // IWYU pragma: export
27#include "arene/base/result/detail/result_base.hpp"
28#include "arene/base/stdlib_choice/addressof.hpp"
29#include "arene/base/stdlib_choice/cstddef.hpp"
30#include "arene/base/stdlib_choice/declval.hpp"
31#include "arene/base/stdlib_choice/enable_if.hpp"
32#include "arene/base/stdlib_choice/forward.hpp"
33#include "arene/base/stdlib_choice/hash.hpp"
34#include "arene/base/stdlib_choice/ignore.hpp"
35#include "arene/base/stdlib_choice/is_constructible.hpp"
36#include "arene/base/stdlib_choice/is_convertible.hpp"
37#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
38#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
39#include "arene/base/stdlib_choice/is_move_assignable.hpp"
40#include "arene/base/stdlib_choice/is_move_constructible.hpp"
41#include "arene/base/stdlib_choice/is_object.hpp"
42#include "arene/base/stdlib_choice/is_same.hpp"
43#include "arene/base/stdlib_choice/is_void.hpp"
44#include "arene/base/stdlib_choice/move.hpp"
45#include "arene/base/stdlib_choice/remove_const.hpp"
46#include "arene/base/stdlib_choice/remove_cv.hpp"
47#include "arene/base/stdlib_choice/remove_reference.hpp"
60// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
61
62namespace arene {
63namespace base {
64
65// parasoft-begin-suppress AUTOSAR-A10_2_1-b-2 "False Positive: Base class function made visible with 'using'"
66// parasoft-begin-suppress AUTOSAR-A1_1_1-b-2 "False Positive: Copy constructor is defined or deleted as appropriate"
67// parasoft-begin-suppress AUTOSAR-M14_5_3-a-2 "False Positive: Copy assignment is defined or deleted as appropriate"
68// parasoft-begin-suppress AUTOSAR-A12_0_1-a-2 "False Positive: Copy assignment is defined or deleted as appropriate"
69/// @brief A sum type which can hold either a _value_ or an _error_. Similar to
70/// [std::expected](https://en.cppreference.com/w/cpp/utility/expected).
71///
72/// @tparam T the type of the value channel. May be @c void , otherwise must satisfy @c std::is_object and must not have
73/// const or volatile qualification.
74/// @tparam E the type of the error channel. May not be @c void , must satisfy @c std::is_object and must not have
75/// const or volatile qualification.
76///
77/// @c result<T,E> can be used as a return type from an API to express the possibility that API being _failable_ without
78/// the need for outparams or exceptions. Its design is non-allocating, and it its memory usage is similar to that of a
79/// @c union : it requires the amount of memory needed to store the larger of @c T or @c E , plus the space for the
80/// discriminator.
81///
82/// Semantically, @c result<T,E> contains two _channels_, the _value channel_ and the _error channel_, and at any point
83/// during program execution exactly **one** of these channels is populated. A default constructed @c result<T,E> has
84/// its @c value_channel default constructed.
85///
86/// The following major operations are supported:
87/// * Querying whether the value or error channel is populated (optionally with a specific value/error).
88/// * Accessing the value/error channel's content. This is a _checked_ operation, meaning that an @c ARENE_PRECONDITION
89/// violation will occur if the specified channel is not populated.
90/// * Equality Comparison if both @c T and @c E are equality comparable. @c results with different channels populated
91/// always compare unequal.
92/// * Assignment if both @c T and @c E are assignable.
93/// * @c swap if both @c T and @c E swappable and move-assignable.
94/// * @c std::hash if both @c T and @c E support @c std::hash .
95/// * Conversion to @c bool , which is equivalent to @c result<T,E>::has_value() .
96/// * Dereferencing the @c result , which is equivalent to accessing @c result::value() .
97///
98/// Some basic example usages:
99/// @snippet docs/examples/result_examples.cpp basic_example
100///
101/// Please see the detailed [user manual](result) for usage examples and best practices.
102template <class T, class E>
103// NOLINTNEXTLINE(hicpp-special-member-functions) false-positive, it can't see through the non-constructible-dummy trick
104class result : result_detail::result_base<T, E> {
105 /// @brief The type of the base class holding the storage
106 using base_type = result_detail::result_base<T, E>;
107
108 static_assert(
110 "The value type stored in result must not have an overloaded address-of operator"
111 );
112
113 public:
114 // parasoft-begin-suppress AUTOSAR-A2_10_1-d-2 "False Positive: This identifier does not hide anything"
115 /// @brief The type when the @c result contains a value.
116 using value_type = T;
117 // parasoft-end-suppress AUTOSAR-A2_10_1-d-2
118
119 /// @brief The type when the @c result contains an error.
120 using error_type = E;
121
122 private:
123 static_assert(
125 "value_type must be an unqualified type, non-reference type."
126 );
127 static_assert(
129 "error_type must be an unqualified type, non-reference type."
130 );
131
132 static_assert(!std::is_same<error_type, void>::value, "void as an error_type is not supported");
133 static_assert(
135 "monostate as an error_type is not supported"
136 );
137
138 static_assert(std::is_object<value_type>::value, "value_type must be an object type (it must not be a reference).");
139 static_assert(std::is_object<error_type>::value, "error_type must be an object type (it must not be a reference).");
140
141 public:
142 ///
143 /// @brief Constructs a @c result with a default-constructed @c value_type . Does not participate in overload
144 /// resolution if @c value_type is not default constructible.
145 /// @post @c has_value() returns @c true
146 /// @post @c value() returns reference to an object equivalent to default-constructing @c value_type .
147 constexpr result() = default;
148
149 ///
150 /// @brief Constructs a @c result containing the @c value_type by way of direct-non-list-initialization.
151 ///
152 /// @tparam P The type of the initializers. Must satisfy @c std::is_constructible<value_type,P&&...> .
153 /// @param args The arguments to initialize the value channel with.
154 /// @post @c has_value() returns @c true
155 /// @post @c value() returns reference to an object equivalent to constructing @c value_type with @c args .
156 ///
157 /// Example Usage:
158 /// @snippet docs/examples/result_examples.cpp in_place_value
159 template <class... P, constraints<std::enable_if_t<std::is_constructible<value_type, P&&...>::value>> = nullptr>
160 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
161 constexpr result(in_place_value_t, P&&... args) noexcept(noexcept(base_type(in_place_value, std::forward<P>(args)...))
162 )
163 : base_type(in_place_value, std::forward<P>(args)...) {}
164
165 ///
166 /// @brief Constructs a @c result containing the @c error_type by way of direct-non-list-initialization.
167 ///
168 /// @tparam P The type of the initializers. Must satisfy @c std::is_constructible<error_type,P&&...> .
169 /// @param args The arguments to initialize the value channel with.
170 /// @post @c has_error() returns @c true
171 /// @post @c error() returns reference to an object equivalent to constructing @c error_type with @c args .
172 ///
173 /// Example Usage:
174 /// @snippet docs/examples/result_examples.cpp in_place_error
175 template <class... P, constraints<std::enable_if_t<std::is_constructible<error_type, P&&...>::value>> = nullptr>
176 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
177 constexpr result(in_place_error_t, P&&... args) noexcept(noexcept(base_type(in_place_error, std::forward<P>(args)...))
178 )
179 : base_type(in_place_error, std::forward<P>(args)...) {}
180
181 /// @brief Default copy constructor
182 constexpr result(result const& copy) noexcept(noexcept(base_type(std::declval<result const&>()))) = default;
183 /// @brief Default move constructor
184 // NOLINTNEXTLINE(hicpp-noexcept-move) We want to exactly mimic the noexcept properties of ValueType and ErrorType.
185 constexpr result(result&& move) noexcept(noexcept(base_type(std::declval<result&&>()))) = default;
186 /// @brief Destroy the stored value or error
187 ~result() = default;
188
189 private:
190 /// @brief trait which detects if the result should be copy-assignable
191 static constexpr bool is_copy_assignable{
194 };
195
196 /// @brief The argument for the "copy assign" operator: @c result if the assignment is possible, @c
197 /// non_constructible_dummy if not
199 /// @brief The argument for the *deleted* "copy assign" operator: @c non_constructible_dummy if the assignment is
200 /// possible, @c result if not (so the copy assignment is deleted)
202
203 public:
204 /// @brief Copy-assignment for the case that @c value_type and @c error_type satisfy
205 /// @c std::is_nothrow_copy_constructible and @c std::is_nothrow_copy_assignable, otherwise a dummy assignment
206 /// operator that can never be called
207 /// @param other The value to copy from
208 /// @return *this
209 constexpr auto operator=(copy_assign_arg const& other) noexcept -> result& {
210 if (&other != this) {
211 std::ignore = assign(other);
212 }
213 return *this;
214 }
215 /// @brief Deleted Copy-assignment if @c value_type or @c error_type do not satisfy
216 /// @c std::is_nothrow_copy_constructible and @c std::is_nothrow_copy_assignable, otherwise a deleted dummy assignment
217 /// operator
218 /// @param other The value to copy from
219 /// @return *this
220 constexpr auto operator=(deleted_copy_assign_arg const& other) noexcept -> result& = delete;
221
222 private:
223 /// @brief trait to detect if the result should be move-assignable
224 static constexpr bool is_move_assignable{
227 };
228
229 /// @brief The argument for the "move assign" operator: @c result if the assignment is possible, @c
230 /// non_constructible_dummy if not
232 /// @brief The argument for the *deleted* "move assign" operator: @c non_constructible_dummy if the assignment is
233 /// possible, @c result if not (so the move assignment is deleted)
235
236 public:
237 /// @brief Move-assignment for the case that @c value_type and @c error_type satisfy
238 /// @c std::is_nothrow_move_constructible and @c std::is_nothrow_move_assignable, otherwise a dummy assignment
239 /// operator that can never be called
240 /// @param other The value to move from
241 /// @return *this
242 constexpr auto operator=(move_assign_arg&& other) noexcept -> result& {
243 std::ignore = assign(std::move(other));
244 return *this;
245 }
246 /// @brief Deleted Move-assignment if @c value_type or @c error_type do not satisfy
247 /// @c std::is_nothrow_move_constructible and @c std::is_nothrow_move_assignable, otherwise a deleted dummy assignment
248 /// operator
249 /// @param other The value to move from
250 /// @return *this
251 constexpr auto operator=(deleted_move_assign_arg&& other) noexcept -> result& = delete;
252
253 // @brief Expose @c has_value from base type
254 using base_type::has_value;
255
256 /// @brief Checks if the value channel contains a specific value
257 ///
258 /// @param val The value to check for.
259 /// @return true If @c has_value() and @c value()==val .
260 /// @return false Otherwise.
261 template <typename U = value_type, constraints<std::enable_if_t<is_equality_comparable_v<U>>> = nullptr>
262 ARENE_NODISCARD constexpr auto has_value(value_type const& val) const
263 noexcept(noexcept(val == std::declval<value_type>())) -> bool {
264 if (this->has_value()) {
265 return val == this->storage().unsafe_value();
266 }
267 return false;
268 }
269
270 // @brief Expose @c has_error from base type
271 using base_type::has_error;
272
273 // @brief Expose boolean conversion from base type
274 using base_type::operator bool;
275
276 /// @brief Accesses a pointer to the content of the value channel.
277 ///
278 /// @pre has_value(), else ARENE_PRECONDITION violation.
279 /// @return A pointer to the contained value
280 ARENE_NODISCARD constexpr auto operator->() noexcept -> value_type* { return &value(); }
281
282 /// @brief Accesses a pointer to the content of the value channel.
283 ///
284 /// @pre has_value(), else ARENE_PRECONDITION violation.
285 /// @return A pointer to the contained value
286 ARENE_NODISCARD constexpr auto operator->() const noexcept -> value_type const* { return &value(); }
287
288 /// @brief Accesses a reference to the content of the value channel.
289 ///
290 /// @pre has_value(), else ARENE_PRECONDITION violation.
291 /// @return A reference to the contained value
292 ARENE_NODISCARD constexpr auto operator*() & noexcept -> value_type& { return value(); }
293
294 /// @brief Accesses a reference to the content of the value channel.
295 ///
296 /// @pre has_value(), else ARENE_PRECONDITION violation.
297 /// @return A const reference to the contained value
298 ARENE_NODISCARD constexpr auto operator*() const& noexcept -> value_type const& { return value(); }
299
300 /// @brief Accesses a reference to the content of the value channel.
301 ///
302 /// @pre has_value(), else ARENE_PRECONDITION violation.
303 /// @return An rvalue reference to the contained value
304 ARENE_NODISCARD constexpr auto operator*() && noexcept -> value_type&& { return std::move(value()); }
305
306 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
307 /// @brief Accesses a reference to the content of the value channel.
308 ///
309 /// @pre has_value(), else ARENE_PRECONDITION violation.
310 /// @return A const rvalue reference to the contained value
311 ARENE_NODISCARD constexpr auto operator*() const&& noexcept -> value_type const&& { return std::move(value()); }
312 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
313
314 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
315 /// @brief Accesses a reference to the content of the value channel.
316 ///
317 /// @pre has_value(), else ARENE_PRECONDITION violation.
318 /// @return A reference to the contained value
319 ARENE_NODISCARD constexpr auto value() & noexcept -> value_type& {
321 return this->storage().unsafe_value();
322 }
323 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
324
325 /// @brief Accesses a reference to the content of the value channel.
326 ///
327 /// @pre has_value(), else ARENE_PRECONDITION violation.
328 /// @return A const reference to the contained value
329 ARENE_NODISCARD constexpr auto value() const& noexcept -> value_type const& {
331 return this->storage().unsafe_value();
332 }
333
334 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
335 /// @brief Accesses a reference to the content of the value channel.
336 ///
337 /// @pre has_value(), else ARENE_PRECONDITION violation.
338 /// @return An rvalue reference to the contained value
339 ARENE_NODISCARD constexpr auto value() && noexcept -> value_type&& {
341 return std::move(this->storage().unsafe_value());
342 }
343 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
344
345 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
346 /// @brief Accesses a reference to the content of the value channel.
347 ///
348 /// @pre has_value(), else ARENE_PRECONDITION violation.
349 /// @return A const rvalue reference to the contained value
350 ARENE_NODISCARD constexpr auto value() const&& noexcept -> value_type const&& {
352 return std::move(this->storage().unsafe_value());
353 }
354 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
355
356 /// @brief Returns the value if the value channel is populated, else another value. Not declared if @c T is @c void .
357 /// @tparam U The type of the default value. Must satisfy @c std::is_convertible<U,value_type> .
358 /// @param default_value The default value to use if the value channel is not populated.
359 /// @return value_type If @c has_value() is true, equivalent to @c value() . Otherwise, @c default_value converted to
360 /// @c value_type .
361 template <typename U = T, constraints<std::enable_if_t<std::is_convertible<U, value_type>::value>> = nullptr>
362 ARENE_NODISCARD constexpr auto value_or(U&& default_value) const& noexcept -> value_type {
363 return this->has_value() ? this->storage().unsafe_value() : static_cast<value_type>(std::forward<U>(default_value));
364 }
365
366 /// @brief Returns the value if the value channel is populated, else another value. Not declared if @c T is @c void .
367 /// @tparam U The type of the default value. Must satisfy @c std::is_convertible<U,value_type> .
368 /// @param default_value The default value to use if the value channel is not populated.
369 /// @return value_type If @c has_value() is true, equivalent to @c value() . Otherwise, @c default_value converted to
370 /// @c value_type .
371 template <typename U = T, constraints<std::enable_if_t<std::is_convertible<U, value_type>::value>> = nullptr>
372 ARENE_NODISCARD constexpr auto value_or(U&& default_value) && noexcept -> value_type {
373 return this->has_value() ? std::move(this->storage().unsafe_value())
374 : static_cast<value_type>(std::forward<U>(default_value));
375 }
376
377 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
378 /// @brief Accesses a reference to the content of the error channel.
379 ///
380 /// @pre has_error(), else ARENE_PRECONDITION violation
381 /// @return A reference to the contained error
382 ARENE_NODISCARD constexpr auto error() & noexcept -> error_type& {
383 ARENE_PRECONDITION(this->has_error());
384 return this->storage().unsafe_error();
385 }
386 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
387
388 /// @brief Accesses a reference to the content of the error channel.
389 ///
390 /// @pre has_error(), else ARENE_PRECONDITION violation
391 /// @return A const reference to the contained error
392 ARENE_NODISCARD constexpr auto error() const& noexcept -> error_type const& {
393 ARENE_PRECONDITION(this->has_error());
394 return this->storage().unsafe_error();
395 }
396 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
397 /// @brief Accesses a reference to the content of the error channel.
398 ///
399 /// @pre has_error(), else ARENE_PRECONDITION violation
400 /// @return An rvalue reference to the contained error
401 ARENE_NODISCARD constexpr auto error() && noexcept -> error_type&& {
402 ARENE_PRECONDITION(this->has_error());
403 return std::move(this->storage().unsafe_error());
404 }
405 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
406 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
407 /// @brief Accesses a reference to the content of the error channel.
408 ///
409 /// @pre has_error(), else ARENE_PRECONDITION violation
410 /// @return A const rvalue reference to the contained error
411 ARENE_NODISCARD constexpr auto error() const&& noexcept -> error_type const&& {
412 ARENE_PRECONDITION(this->has_error());
413 return std::move(this->storage().unsafe_error());
414 }
415 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
416
417 /// @brief Swaps the state of two results.
418 ///
419 /// @tparam SourceValueType Must satisfy @c is_swappable_v<SourceValueType> and @c
420 /// std::is_nothrow_move_constructible<SourceValueType>
421 /// @tparam SourceErrorType Must satisfy @c is_swappable_v<SourceErrorType> and @c
422 /// std::is_nothrow_move_constructible<SourceErrorType>
423 /// @param other The result to swap with
424 /// @post If both @c this and @c other have matching channels populated, the content of those channels are swapped as
425 /// if via @c arene::base::swap . Otherwise, it is equivalent to emplacing @c this with the active channel
426 /// from @c other , and vice-versa.
427 ///
428 template <
429 typename SourceValueType = T,
430 typename SourceErrorType = E,
436 constexpr void swap(result& other
438 if (this->has_value()) {
439 if (other.has_value()) {
440 ::arene::base::swap(this->storage().unsafe_value(), other.storage().unsafe_value());
441 } else {
442 error_type tmp{std::move(other.storage().unsafe_error())};
443 static_cast<void>(other.emplace(in_place_value, std::move(this->storage().unsafe_value())));
444 static_cast<void>(this->emplace(in_place_error, std::move(tmp)));
445 }
446 } else if (this->has_error()) {
447 if (other.has_error()) {
448 ::arene::base::swap(this->storage().unsafe_error(), other.storage().unsafe_error());
449 } else {
450 value_type tmp{std::move(other.storage().unsafe_value())};
451 static_cast<void>(other.emplace(in_place_error, std::move(this->storage().unsafe_error())));
452 static_cast<void>(this->emplace(in_place_value, std::move(tmp)));
453 }
454 } else {
455 // do nothing
456 }
457 }
458
459 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "Hidden friends permitted by A11-3-1 Permit #2"
460 /// @brief Swaps the state of two results.
461 ///
462 /// @tparam SourceValueType Must satisfy @c is_swappable_v<SourceValueType> , @c
463 /// std::is_nothrow_move_constructible<SourceValueType>
464 /// @tparam SourceErrorType Must satisfy @c is_swappable_v<SourceErrorType> , @c
465 /// std::is_nothrow_move_constructible<SourceErrorType>
466 /// @param lhs The left hand result to swap
467 /// @param rhs The right hand result to swap
468 /// @post If both @c lhs and @c rhs have matching channels populated, the content of those channels are swapped as
469 /// if via @c arene::base::swap . Otherwise, it is equivalent to emplacing @c lhs with the active channel
470 /// from @c rhs , and vise-versa.
471 ///
472 template <
473 typename SourceValueType = T,
474 typename SourceErrorType = E,
480 friend constexpr void swap(result& lhs, result& rhs) noexcept(noexcept(lhs.swap(rhs))) {
481 lhs.swap(rhs);
482 }
483 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
484
485 // imports emplace from result_base.
486 using base_type::emplace;
487
488 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
489 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
490 /// @brief Emplaces a value into the result
491 ///
492 /// @tparam ArgTypes the types of the arguments to construct the value with
493 /// @param args The arguments to construct the value with
494 /// @return A reference to the newly emplaced value
495 /// @post @c has_value() returns @c true
496 /// @post The content of @c value() will be equivalent to a @c value_type direct-initialized from @c args .
497 ///
498 template <
499 typename... ArgTypes,
501 auto emplace(in_place_value_t, ArgTypes&&... args) noexcept -> value_type& {
502 this->reset();
503 this->discriminator() = result_detail::discriminator_type::holds_value;
504 return *::arene::base::construct_at(
505 std::addressof(this->storage().unsafe_value()),
507 );
508 }
509 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
510 // parasoft-end-suppress CERT_C-EXP37-a-3
511
512 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
513 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
514 /// @brief Monadic API which invokes a functor with the contents of @c value() if the value channel is populated.
515 ///
516 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
517 /// of @c SelfType, and return @c result<U,error_type> , where @c U may be any valid @c value_type .
518 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
519 /// @return result<U, error_type> which will be either the result of invoking @c handle_value with the contents of
520 /// @c self.value() , or it will contain a copy of @c self.error() .
521 ///
522 /// Basic usage example for opening a file:
523 /// @snippet docs/examples/result_examples.cpp and_then
524 ///
525 /// After this statement, @c maybe_file will contain either a valid instance of @c arene::base::file_handle in the
526 /// value channel, or it will contain an instance of @c arene::base::error_code representing either the failure to
527 /// open the file or the failure to open the directory.
528 ///
529 /// @{
530 template <typename F>
531 constexpr auto and_then(F&& handle_value) & {
532 return result::and_then(*this, std::forward<F>(handle_value));
533 }
534 template <typename F>
535 constexpr auto and_then(F&& handle_value) const& {
536 return result::and_then(*this, std::forward<F>(handle_value));
537 }
538 template <typename F>
539 constexpr auto and_then(F&& handle_value) && {
540 return result::and_then(std::move(*this), std::forward<F>(handle_value));
541 }
542 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
543 template <typename F>
544 constexpr auto and_then(F&& handle_value) const&& {
545 return result::and_then(std::move(*this), std::forward<F>(handle_value));
546 }
547 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
548 /// @}
549 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
550 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
551
552 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
553 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
554 /// @brief Monadic API which invokes a functor with the contents of @c error() if the error channel is populated.
555 ///
556 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
557 /// of @c SelfType and return @c result<value_type,E> , where @c E may be any valid @c error_type .
558 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
559 /// @return result<value_type, E> which will be either the result of invoking @c handle_error with the contents of
560 /// @c self.error() , or it will contain a copy of @c self.value() .
561 ///
562 /// Basic usage example for opening a directory:
563 /// @snippet docs/examples/result_examples.cpp or_else
564 ///
565 /// @{
566 template <typename F>
567 constexpr auto or_else(F&& handle_error) & {
568 return result::or_else(*this, std::forward<F>(handle_error));
569 }
570 template <typename F>
571 constexpr auto or_else(F&& handle_error) const& {
572 return result::or_else(*this, std::forward<F>(handle_error));
573 }
574 template <typename F>
575 constexpr auto or_else(F&& handle_error) && {
576 return result::or_else(std::move(*this), std::forward<F>(handle_error));
577 }
578 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
579 template <typename F>
580 constexpr auto or_else(F&& handle_error) const&& {
581 return result::or_else(std::move(*this), std::forward<F>(handle_error));
582 }
583 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
584 /// @}
585 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
586 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
587
588 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
589 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
590 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
591 /// @brief Calls a provided functor with the value channel if it is populated, and produces a new result with the
592 /// return from it.
593 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
594 /// of @c this . May return any valid @c value_type
595 /// @param handle_value the functor to invoke if @c has_value() is @c true .
596 /// @return result<U, error_type> which will be one of:
597 /// * A @c result<U,error_type> constructed from the result of invoking @c handle_value with the contents of
598 /// @c value() if @c value_type is not @c void and the return from @c F is not @c void
599 /// * A @c result<void,error_type> constructed from the result of invoking @c handle_value with the contents
600 /// of @c value() if @c value_type is not @c void and the return from @c F is @c void
601 /// * A @c result<U,error_type> where @c U is the return type of @c F constructed from a copy of @c error() .
602 /// @{
603 template <typename F>
604 constexpr auto transform(F&& handle_value) & {
605 return result::transform(*this, std::forward<F>(handle_value));
606 }
607 template <typename F>
608 constexpr auto transform(F&& handle_value) const& {
609 return result::transform(*this, std::forward<F>(handle_value));
610 }
611 template <typename F>
612 constexpr auto transform(F&& handle_value) && {
613 return result::transform(std::move(*this), std::forward<F>(handle_value));
614 }
615 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
616 template <typename F>
617 constexpr auto transform(F&& handle_value) const&& {
618 return result::transform(std::move(*this), std::forward<F>(handle_value));
619 }
620 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
621 /// @}
622 // parasoft-end-suppress AUTOSAR-A2_10_1-d
623 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
624 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
625
626 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
627 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
628 /// @brief Calls a provided functor with the error channel if it is populated, and produces a new result with the
629 /// return from it.
630 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
631 /// of @c this. May return any valid @c error_type .
632 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
633 /// @return result<value_type, E> which will be either:
634 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
635 /// * A result constructed from a copy of @c self.value() .
636 /// @{
637 template <typename F>
638 constexpr auto transform_error(F&& handle_error) & {
639 return result::transform_error(*this, std::forward<F>(handle_error));
640 }
641 template <typename F>
642 constexpr auto transform_error(F&& handle_error) const& {
643 return result::transform_error(*this, std::forward<F>(handle_error));
644 }
645 template <typename F>
646 constexpr auto transform_error(F&& handle_error) && {
647 return result::transform_error(std::move(*this), std::forward<F>(handle_error));
648 }
649 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
650 template <typename F>
651 constexpr auto transform_error(F&& handle_error) const&& {
652 return result::transform_error(std::move(*this), std::forward<F>(handle_error));
653 }
654 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
655 /// @}
656 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
657 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
658
659 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function used in other TUs"
660 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Conditionally noexcept based on underlying comparison"
661 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
662 /// @brief Compare two results for equality.
663 /// @param lhs The first result
664 /// @param rhs The second result
665 /// @return bool @c true if the both @c lhs and @c rhs hold values that are equal, or both hold errors that are
666 /// equal, @c false otherwise
667 ARENE_NODISCARD friend constexpr auto operator==(result const& lhs, result const& rhs) noexcept(
668 noexcept(lhs.has_value(rhs.value())) && noexcept(lhs.has_error(rhs.error()))
669 ) -> bool {
670 static_assert(is_equality_comparable_v<value_type>, "Value type must be equality comparable");
671 static_assert(is_equality_comparable_v<error_type>, "Error type must be equality comparable");
672 if (lhs.has_value()) {
673 return rhs.has_value(lhs.storage().unsafe_value());
674 }
675 return rhs.has_error(lhs.storage().unsafe_error());
676 }
677 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
678 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
679 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
680
681 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function used in other TUs"
682 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Conditionally noexcept based on underlying comparison"
683 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
684 /// @brief Compare two results for inequality.
685 /// @param lhs The first result
686 /// @param rhs The second result
687 /// @return bool @c true if the both @c lhs and @c rhs hold values that are not equal, or both hold errors that are
688 /// not equal, or one holds a value and the other holds an error, @c false otherwise
689 ARENE_NODISCARD friend constexpr auto operator!=(result const& lhs, result const& rhs) noexcept(noexcept(lhs == rhs))
690 -> bool {
691 return !(lhs == rhs);
692 }
693 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
694 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
695 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
696
697 private:
698 /// @brief Helper for implementing assignment which handles copy/move.
699 /// @tparam OtherType the result type to assign from. @c OtherType::value_type and @c OtherType::error_type must be
700 /// nothrow constructible and nothrow assignable to @c value_type and @c error_type respectably.
701 /// @param other the result to assign from.
702 /// @post The state of @c this will be as follows:
703 /// * If both @c this and @c other 's value channels are populated, then @c this will have its value channel's
704 /// state set as if via assignment from @c other 's value channel.
705 /// * If both @c this and @c other 's error channels are populated, then @c this will have its error channel's
706 /// state set as if via assignment from @c other 's error channel.
707 /// * Otherwise, @c this will have its state set as if @c emplace 'd with the active channel of @c other .
708 /// @return result& A reference to @c this post assignment.
709 template <typename OtherType, constraints<std::enable_if_t<decays_to_v<OtherType, result>>> = nullptr>
710 constexpr auto assign(OtherType&& other) noexcept -> result& {
711 if (this->has_value()) {
712 if (other.has_value()) {
713 this->storage().unsafe_value() =
714 forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_value());
715 return *this;
716 }
717 std::ignore = this->emplace(
718 in_place_error,
719 forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_error())
720 );
721 return *this;
722 }
723 if (other.has_error()) {
724 this->storage().unsafe_error() = forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_error());
725 return *this;
726 }
728 this->emplace(in_place_value, forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_value()));
729 return *this;
730 }
731
732 ///
733 /// @brief deduced-this helper for @c result::and_then<F>(F&&) .
734 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
735 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
736 /// of @c SelfType , and return @c result<U,error_type> , where @c U may be any valid @c value_type .
737 /// @param self the instance of the result to use.
738 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
739 /// @return result<U, error_type> which will be either the result of invoking @c handle_value with the contents of
740 /// @c self.value() , or it will contain a copy of @c self.error() .
741 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
742 static constexpr auto and_then(SelfType&& self, F&& handle_value) {
743 using f_result_t =
745 static_assert(is_instantiation_of_v<f_result_t, ::arene::base::result>, "handle_value must return a result!");
746 static_assert(
747 std::is_same<typename f_result_t::error_type, typename remove_cvref_t<SelfType>::error_type>::value,
748 "The result returned from handle_value's error channel must be the same as this result's error channel"
749 );
750 if (self.has_value()) {
751 return ::arene::base::invoke(
752 std::forward<F>(handle_value),
753 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_value())
754 );
755 }
756 return f_result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
757 }
758
759 ///
760 /// @brief deduced-this helper for @c result::or_else<F>(F&&) .
761 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
762 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
763 /// of @c SelfType and return @c result<value_type,E> , where @c E may be any valid @c error_type .
764 /// @param self the instance of the result to use.
765 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
766 /// @return result<value_type, E> which will be either the result of invoking @c handle_error with the contents of
767 /// @c self.error() , or it will contain a copy of @c self.value() .
768 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
769 static constexpr auto or_else(SelfType&& self, F&& handle_error) {
770 using f_result_t =
772 static_assert(is_instantiation_of_v<f_result_t, ::arene::base::result>, "handle_error must return a result!");
773 static_assert(
775 "The result returned from handle_error's value channel must be the same as this result's value channel"
776 );
777 if (self.has_error()) {
778 return ::arene::base::invoke(
779 std::forward<F>(handle_error),
780 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())
781 );
782 }
783 return f_result_t{in_place_value, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_value())};
784 }
785
786 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it isn't hiding anything."
787 /// @brief deduced-this helper for @c result::transform<F>(F&&) .
788 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
789 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
790 /// of @c SelfType. May return any valid @c value_type
791 /// @param self the instance of the result to use.
792 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
793 /// @return result<U, error_type> which will be one of:
794 /// * A @c result<U,error_type> constructed from the result of invoking @c handle_value with the contents of
795 /// @c self.value()
796 /// * A @c result<U,error_type> where @c U is the return type of @c F constructed from a copy of @c error() .
797 template <
798 typename SelfType,
799 typename F,
801 std::enable_if<decays_to_v<SelfType, result>>,
802 std::enable_if_t<!std::is_void<
804 value>> = nullptr>
805 static constexpr auto transform(SelfType&& self, F&& handle_value) {
806 using f_value_t =
808 using result_t = result<f_value_t, error_type>;
809 if (self.has_value()) {
810 return result_t{
811 in_place_value,
813 std::forward<F>(handle_value),
814 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_value())
815 )
816 };
817 }
818 return result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
819 }
820 /// @brief deduced-this helper for @c result::transform<F>(F&&) .
821 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
822 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
823 /// of @c SelfType. May return any valid @c value_type
824 /// @param self the instance of the result to use.
825 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
826 /// @return result<void, error_type> which will be one of:
827 /// * A @c result<void,error_type> constructed from the result of invoking @c handle_value with the contents
828 /// of @c self.value()
829 /// * A @c result<void,error_type> constructed from a copy of @c error() .
830 template <
831 typename SelfType,
832 typename F,
834 std::enable_if<decays_to_v<SelfType, result>>,
835 std::enable_if_t<std::is_void<
837 value>> = nullptr>
838 static constexpr auto transform(SelfType&& self, F&& handle_value) {
839 using result_t = result<void, error_type>;
840 if (self.has_value()) {
841 ::arene::base::invoke(
842 std::forward<F>(handle_value),
843 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_value())
844 );
845 return result_t{};
846 }
847 return result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
848 }
849 // parasoft-end-suppress AUTOSAR-A2_10_1-d
850
851 ///
852 /// @brief deduced-this helper for @c result::transform_error<F>(F&&) .
853 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
854 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
855 /// of @c SelfType. May return any valid @c error_type .
856 /// @param self the instance of the result to use.
857 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
858 /// @return result<value_type, E> which will be either:
859 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
860 /// * A result constructed from a copy of @c self.value() .
861 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
862 static constexpr auto transform_error(SelfType&& self, F&& handle_error) {
863 using f_error_t =
865 using result_t = result<value_type, f_error_t>;
866 if (self.has_error()) {
867 return result_t{
868 in_place_error,
870 std::forward<F>(handle_error),
871 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())
872 )
873 };
874 }
875 return result_t{in_place_value, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_value())};
876 }
877}; // result<T,E>
878// parasoft-end-suppress AUTOSAR-A12_0_1-a-2
879// parasoft-end-suppress AUTOSAR-M14_5_3-a-2
880// parasoft-end-suppress AUTOSAR-A1_1_1-b-2
881// parasoft-end-suppress AUTOSAR-A10_2_1-b-2
882
883// parasoft-begin-suppress AUTOSAR-A10_2_1-b-2 "False Positive: Base class function made visible with 'using'"
884// parasoft-begin-suppress AUTOSAR-A1_1_1-b-2 "False Positive: Copy constructor is defined or deleted as appropriate"
885// parasoft-begin-suppress AUTOSAR-M14_5_3-a-2 "False Positive: Copy assignment is defined or deleted as appropriate"
886// parasoft-begin-suppress AUTOSAR-A12_0_1-a-2 "False Positive: Copy assignment is defined or deleted as appropriate"
887/// @brief Specialization of result<T, E> for void value types.
888/// @tparam E The error type
889template <class E>
890// NOLINTNEXTLINE(hicpp-special-member-functions) false-positive, it can't see through the non-constructible-dummy trick
891class result<void, E> : result_detail::result_base<monostate, E> {
892 /// @brief The type of the base class holding the storage
893 using base_type = result_detail::result_base<monostate, E>;
894
895 public:
896 // parasoft-begin-suppress AUTOSAR-A2_10_1-d-2 "False Positive: This identifier does not hide anything"
897 /// @brief The type when the @c result contains a value.
898 using value_type = void;
899 // parasoft-end-suppress AUTOSAR-A2_10_1-d-2
900
901 /// @brief The type when the @c result contains an error.
902 using error_type = E;
903
904 private:
905 static_assert(
907 "value_type must be an unqualified type, non-reference type."
908 );
909 static_assert(
911 "error_type must be an unqualified type, non-reference type."
912 );
913
914 static_assert(!std::is_same<error_type, void>::value, "void as an error_type is not supported");
915 static_assert(
917 "monostate as an error_type is not supported"
918 );
919
920 static_assert(std::is_object<error_type>::value, "error_type must be an object type (it must not be a reference).");
921
922 public:
923 ///
924 /// @brief Constructs a @c result with the value-channel engaged.
925 /// @post @c has_value() returns @c true
926 constexpr result() = default;
927
928 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
929 /// @brief Constructs a @c result with the value-channel engaged.
930 ///
931 /// @tparam P The type of the initializers. Must satisfy @c std::is_constructible<value_type,P&&...> .
932 /// @post @c has_value() returns @c true
933 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
934 constexpr result(in_place_value_t) noexcept(noexcept(base_type(in_place_value, monostate{})))
935 : base_type(in_place_value, monostate{}) {}
936 /// @brief Constructs a @c result with the value-channel engaged. Provided for backwards compatibility.
937 ///
938 /// @tparam P The type of the initializers. Must satisfy @c std::is_constructible<value_type,P&&...> .
939 /// @post @c has_value() returns @c true
940 constexpr result(in_place_value_t, monostate) noexcept(noexcept(base_type(in_place_value, monostate{})))
941 : base_type(in_place_value, monostate{}) {}
942 // parasoft-end-suppress CERT_C-EXP37-a-3
943
944 ///
945 /// @brief Constructs a @c result containing the @c error_type by way of direct-non-list-initialization.
946 ///
947 /// @tparam P The type of the initializers. Must satisfy @c std::is_constructible<error_type,P&&...> .
948 /// @param args The arguments to initialize the value channel with.
949 /// @post @c has_error() returns @c true
950 /// @post @c error() returns reference to an object equivalent to constructing @c error_type with @c args .
951 ///
952 /// Example Usage:
953 /// @snippet docs/examples/result_examples.cpp in_place_error_void
954 template <class... P, constraints<std::enable_if_t<std::is_constructible<error_type, P&&...>::value>> = nullptr>
955 // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
956 constexpr result(in_place_error_t, P&&... args) noexcept(noexcept(base_type(in_place_error, std::forward<P>(args)...))
957 )
958 : base_type(in_place_error, std::forward<P>(args)...) {}
959
960 /// @brief Default copy constructor
961 constexpr result(result const& copy) noexcept(noexcept(base_type(std::declval<result const&>()))) = default;
962 /// @brief Default move constructor
963 // NOLINTNEXTLINE(hicpp-noexcept-move) We want to exactly mimic the noexcept properties of ValueType and ErrorType.
964 constexpr result(result&& move) noexcept(noexcept(base_type(std::declval<result&&>()))) = default;
965 /// @brief Destroy the stored value or error
966 ~result() = default;
967
968 private:
969 /// @brief trait which detects if the result should be copy-assignable
970 static constexpr bool is_copy_assignable{
972 };
973
974 /// @brief The argument for the "copy assign" operator: @c result if the assignment is possible, @c
975 /// non_constructible_dummy if not
977 /// @brief The argument for the *deleted* "copy assign" operator: @c non_constructible_dummy if the assignment is
978 /// possible, @c result if not (so the copy assignment is deleted)
980
981 public:
982 /// @brief Copy-assignment for the case that @c error_type satisfies @c std::is_nothrow_copy_constructible and @c
983 /// std::is_nothrow_copy_assignable, otherwise a dummy assignment operator that can never be called
984 /// @param other The value to copy from
985 /// @return *this
986 constexpr auto operator=(copy_assign_arg const& other) noexcept -> result& {
987 if (&other != this) {
988 std::ignore = assign(other);
989 }
990 return *this;
991 }
992 /// @brief Deleted Copy-assignment if @c error_type does not satisfy @c std::is_nothrow_copy_constructible and @c
993 /// std::is_nothrow_copy_assignable, otherwise a deleted dummy assignment operator
994 /// @param other The value to copy from
995 /// @return *this
996 constexpr auto operator=(deleted_copy_assign_arg const& other) noexcept -> result& = delete;
997
998 private:
999 /// @brief trait to detect if the result should be move-assignable
1000 static constexpr bool is_move_assignable{
1002 };
1003
1004 /// @brief The argument for the "move assign" operator: @c result if the assignment is possible, @c
1005 /// non_constructible_dummy if not
1007 /// @brief The argument for the *deleted* "move assign" operator: @c non_constructible_dummy if the assignment is
1008 /// possible, @c result if not (so the move assignment is deleted)
1010
1011 public:
1012 /// @brief Move-assignment for the case that @c error_type satisfies @c std::is_nothrow_move_constructible and @c
1013 /// std::is_nothrow_move_assignable, otherwise a dummy assignment operator that can never be called
1014 /// @param other The value to move from
1015 /// @return *this
1016 constexpr auto operator=(move_assign_arg&& other) noexcept -> result& {
1017 std::ignore = assign(std::move(other));
1018 return *this;
1019 }
1020 /// @brief Deleted Move-assignment if @c error_type does not satisfy @c std::is_nothrow_move_constructible and @c
1021 /// std::is_nothrow_move_assignable, otherwise a deleted dummy assignment operator
1022 /// @param other The value to move from
1023 /// @return *this
1024 constexpr auto operator=(deleted_move_assign_arg&& other) noexcept -> result& = delete;
1025
1026 // @brief Use @c has_value from base type
1027 using base_type::has_value;
1028
1029 // @brief Use @c has_value from base type
1030 using base_type::has_error;
1031
1032 // @brief Use boolean conversion from base type
1033 using base_type::operator bool;
1034
1035 /// @brief Access the content of the value channel.
1036 ///
1037 /// @pre has_value(), else ARENE_PRECONDITION violation.
1038 constexpr void operator*() const& noexcept { return value(); }
1039
1040 /// @brief Access the content of the value channel.
1041 ///
1042 /// @pre has_value(), else ARENE_PRECONDITION violation.
1043 constexpr void value() const& noexcept { ARENE_PRECONDITION(this->has_value()); }
1044 /// @brief Access the content of the value channel.
1045 ///
1046 /// @pre has_value(), else ARENE_PRECONDITION violation.
1047 constexpr void value() && noexcept { ARENE_PRECONDITION(this->has_value()); }
1048
1049 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
1050 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
1051 /// @brief Accesses a reference to the content of the error channel.
1052 ///
1053 /// @pre has_error(), else ARENE_PRECONDITION violation
1054 /// @return A reference to the contained error
1055 /// @{
1056 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
1057 ARENE_NODISCARD constexpr auto error() & noexcept -> error_type& {
1058 ARENE_PRECONDITION(this->has_error());
1059 return this->storage().unsafe_error();
1060 }
1061 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
1062 ARENE_NODISCARD constexpr auto error() const& noexcept -> error_type const& {
1063 ARENE_PRECONDITION(this->has_error());
1064 return this->storage().unsafe_error();
1065 }
1066 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "False positive: This class is a container of the value"
1067 ARENE_NODISCARD constexpr auto error() && noexcept -> error_type&& {
1068 ARENE_PRECONDITION(this->has_error());
1069 return std::move(this->storage().unsafe_error());
1070 }
1071 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
1072 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
1073 ARENE_NODISCARD constexpr auto error() const&& noexcept -> error_type const&& {
1074 ARENE_PRECONDITION(this->has_error());
1075 return std::move(this->storage().unsafe_error());
1076 }
1077 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
1078 /// @}
1079 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
1080 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
1081
1082 ///
1083 /// @brief Swaps the state of two results.
1084 ///
1085 /// @tparam SourceErrorType Must satisfy @c is_swappable_v<error_type> and @c
1086 /// std::is_nothrow_move_constructable<error_type>
1087 /// @param other The result to swap with
1088 /// @post If both @c this and @c other have matching channels populated, the content of those channels are swapped as
1089 /// if via @c arene::base::swap . Otherwise, it is equivalent to emplacing @c this with the active channel
1090 /// from @c other , and vice-versa.
1091 ///
1092 template <
1093 typename SourceErrorType = E,
1097 constexpr void swap(result& other) noexcept(is_nothrow_swappable_v<SourceErrorType>) {
1098 if (this->has_value()) {
1099 if (other.has_value()) {
1100 // swapping voids isn't meaningful
1101 return;
1102 }
1103 static_cast<void>(this->emplace(in_place_error, std::move(other.storage().unsafe_error())));
1104 other.emplace(in_place_value);
1105 } else if (this->has_error()) {
1106 if (other.has_error()) {
1107 ::arene::base::swap(this->storage().unsafe_error(), other.storage().unsafe_error());
1108 } else {
1109 static_cast<void>(other.emplace(in_place_error, std::move(this->storage().unsafe_error())));
1110 this->emplace(in_place_value);
1111 }
1112 } else {
1113 // nothing to do
1114 }
1115 }
1116
1117 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "Hidden friends permitted by A11-3-1 Permit #2"
1118 /// @brief Swaps the state of two results.
1119 ///
1120 /// @tparam SourceErrorType Must satisfy @c is_swappable_v<error_type> and @c
1121 /// std::is_nothrow_move_constructable<error_type>
1122 /// @param lhs The left hand result to swap
1123 /// @param rhs The right hand result to swap
1124 /// @post If both @c lhs and @c rhs have matching channels populated, the content of those channels are swapped as
1125 /// if via @c arene::base::swap . Otherwise, it is equivalent to move-assigning @c lhs with the active channel
1126 /// from @c rhs , and vise-versa.
1127 ///
1128 template <
1129 typename SourceErrorType = E,
1133 friend constexpr void swap(result& lhs, result& rhs) noexcept(noexcept(lhs.swap(rhs))) {
1134 lhs.swap(rhs);
1135 }
1136 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
1137
1138 // imports emplace from result_base.
1139 using base_type::emplace;
1140
1141 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
1142 ///
1143 /// @brief Emplaces the void value into the result
1144 /// @post @c has_value() returns @c true .
1145 ///
1146 void emplace(in_place_value_t) noexcept {
1147 this->reset();
1148 this->discriminator() = result_detail::discriminator_type::holds_value;
1149 ::arene::base::construct_at(std::addressof(this->storage().unsafe_value()), monostate{});
1150 }
1151 // parasoft-end-suppress CERT_C-EXP37-a-3
1152
1153 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
1154 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
1155 /// @brief Monadic API which invokes a functor with the contents of @c value() if the value channel is populated.
1156 ///
1157 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1158 /// of @c SelfType , or with no arguments if @c value_type is @c void , and return @c result<U,error_type> ,
1159 /// where
1160 /// @c U may be any valid @c value_type .
1161 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
1162 /// @return result<U, error_type> which will be either the result of invoking @c handle_value with the contents of
1163 /// @c self.value() , or it will contain a copy of @c self.error() .
1164 ///
1165 /// Basic usage example for opening a file:
1166 /// @snippet docs/examples/result_examples.cpp and_then
1167 ///
1168 /// After this statement, @c maybe_file will contain either a valid instance of @c arene::base::file_handle in the
1169 /// value channel, or it will contain an instance of @c arene::base::error_code representing either the failure to
1170 /// open the file or the failure to open the directory.
1171 ///
1172 /// @{
1173 template <typename F>
1174 constexpr auto and_then(F&& handle_value) & {
1175 return result::and_then(*this, std::forward<F>(handle_value));
1176 }
1177 template <typename F>
1178 constexpr auto and_then(F&& handle_value) const& {
1179 return result::and_then(*this, std::forward<F>(handle_value));
1180 }
1181 template <typename F>
1182 constexpr auto and_then(F&& handle_value) && {
1183 return result::and_then(std::move(*this), std::forward<F>(handle_value));
1184 }
1185 template <typename F>
1186 constexpr auto and_then(F&& handle_value) const&& {
1187 return result::and_then(std::move(*this), std::forward<F>(handle_value));
1188 }
1189 /// @}
1190 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
1191 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
1192
1193 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
1194 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
1195 /// @brief Monadic API which invokes a functor with the contents of @c error() if the error channel is populated.
1196 ///
1197 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1198 /// of @c SelfType and return @c result<value_type,E> , where @c E may be any valid @c error_type .
1199 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1200 /// @return result<value_type, E> which will be either the result of invoking @c handle_error with the contents of
1201 /// @c self.error() , or it will contain a copy of @c self.value() .
1202 ///
1203 /// Basic usage example for opening a directory:
1204 /// @snippet docs/examples/result_examples.cpp or_else
1205 ///
1206 /// After this statement, @c maybe_dir will contain either a valid instance of @c arene::base::directory_handle in
1207 /// the value channel, or it will have logged the error and contain the passed through @c error_code .
1208 ///
1209 /// @{
1210 template <typename F>
1211 constexpr auto or_else(F&& handle_error) & {
1212 return result::or_else(*this, std::forward<F>(handle_error));
1213 }
1214 template <typename F>
1215 constexpr auto or_else(F&& handle_error) const& {
1216 return result::or_else(*this, std::forward<F>(handle_error));
1217 }
1218 template <typename F>
1219 constexpr auto or_else(F&& handle_error) && {
1220 return result::or_else(std::move(*this), std::forward<F>(handle_error));
1221 }
1222 template <typename F>
1223 constexpr auto or_else(F&& handle_error) const&& {
1224 return result::or_else(std::move(*this), std::forward<F>(handle_error));
1225 }
1226 /// @}
1227 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
1228 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
1229
1230 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
1231 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
1232 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
1233 /// @brief Calls a provided functor with the value channel if it is populated, and produces a new result with the
1234 /// return from it.
1235 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1236 /// of @c this , or with no arguments if @c value_type is @c void . May return any valid @c value_type
1237 /// @param handle_value the functor to invoke if @c has_value() is @c true .
1238 /// @return result<U, error_type> which will be one of:
1239 /// * A @c result<U,error_type> constructed from the result of invoking @c handle_value if the return from
1240 /// @c F is not @c void
1241 /// * A @c result<void,error_type> constructed from the result of invoking @c handle_value if the return from
1242 /// @c F is @c void
1243 /// * A @c result<U,error_type> where @c U is the return type of @c F constructed from a copy of
1244 /// @c self.error() .
1245 /// @{
1246 template <typename F>
1247 constexpr auto transform(F&& handle_value) & {
1248 return result::transform(*this, std::forward<F>(handle_value));
1249 }
1250 template <typename F>
1251 constexpr auto transform(F&& handle_value) const& {
1252 return result::transform(*this, std::forward<F>(handle_value));
1253 }
1254 template <typename F>
1255 constexpr auto transform(F&& handle_value) && {
1256 return result::transform(std::move(*this), std::forward<F>(handle_value));
1257 }
1258 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
1259 template <typename F>
1260 constexpr auto transform(F&& handle_value) const&& {
1261 return result::transform(std::move(*this), std::forward<F>(handle_value));
1262 }
1263 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
1264 /// @}
1265 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
1266 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
1267 // parasoft-end-suppress AUTOSAR-A2_10_1-d
1268
1269 /// @brief Calls a provided functor with the error channel if it is populated, and produces a new result with the
1270 /// return from it.
1271 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1272 /// of @c this. May return any valid @c error_type .
1273 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1274 /// @return result<value_type, E> which will be either:
1275 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
1276 /// * A result constructed from a copy of @c self.value() .
1277 /// Basic usage example for opening a directory:
1278 /// @snippet docs/examples/result_examples.cpp transform_error
1279 ///
1280 /// After this statement, @c maybe_dir will contain either a valid instance of @c arene::base::directory_handle in
1281 /// the value channel, or it will have logged the error and contain the passed through @c error_code .
1282 template <typename F>
1283 constexpr auto transform_error(F&& handle_error) & {
1284 return result::transform_error(*this, std::forward<F>(handle_error));
1285 }
1286 /// @brief Calls a provided functor with the error channel if it is populated, and produces a new result with the
1287 /// return from it.
1288 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1289 /// of @c this. May return any valid @c error_type .
1290 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1291 /// @return result<value_type, E> which will be either:
1292 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
1293 /// * A result constructed from a copy of @c self.value() .
1294 /// Basic usage example for opening a directory:
1295 /// @snippet docs/examples/result_examples.cpp transform_error
1296 ///
1297 /// After this statement, @c maybe_dir will contain either a valid instance of @c arene::base::directory_handle in
1298 /// the value channel, or it will have logged the error and contain the passed through @c error_code .
1299 template <typename F>
1300 constexpr auto transform_error(F&& handle_error) const& {
1301 return result::transform_error(*this, std::forward<F>(handle_error));
1302 }
1303 /// @brief Calls a provided functor with the error channel if it is populated, and produces a new result with the
1304 /// return from it.
1305 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1306 /// of @c this. May return any valid @c error_type .
1307 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1308 /// @return result<value_type, E> which will be either:
1309 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
1310 /// * A result constructed from a copy of @c self.value() .
1311 /// Basic usage example for opening a directory:
1312 /// @snippet docs/examples/result_examples.cpp transform_error
1313 ///
1314 /// After this statement, @c maybe_dir will contain either a valid instance of @c arene::base::directory_handle in
1315 /// the value channel, or it will have logged the error and contain the passed through @c error_code .
1316 template <typename F>
1317 constexpr auto transform_error(F&& handle_error) && {
1318 return result::transform_error(std::move(*this), std::forward<F>(handle_error));
1319 }
1320 /// @brief Calls a provided functor with the error channel if it is populated, and produces a new result with the
1321 /// return from it.
1322 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1323 /// of @c this. May return any valid @c error_type .
1324 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1325 /// @return result<value_type, E> which will be either:
1326 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
1327 /// * A result constructed from a copy of @c self.value() .
1328 /// Basic usage example for opening a directory:
1329 /// @snippet docs/examples/result_examples.cpp transform_error
1330 ///
1331 /// After this statement, @c maybe_dir will contain either a valid instance of @c arene::base::directory_handle in
1332 /// the value channel, or it will have logged the error and contain the passed through @c error_code .
1333 template <typename F>
1334 constexpr auto transform_error(F&& handle_error) const&& {
1335 return result::transform_error(std::move(*this), std::forward<F>(handle_error));
1336 }
1337
1338 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function used in other TUs"
1339 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Conditionally noexcept based on underlying comparison"
1340 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
1341 /// @brief Compare two results for equality.
1342 /// @param lhs The first result
1343 /// @param rhs The second result
1344 /// @return bool @c true if the both @c lhs and @c rhs hold values that are equal, or both hold errors that are
1345 /// equal, @c false otherwise
1346 ARENE_NODISCARD friend constexpr auto operator==(result const& lhs, result const& rhs) noexcept(
1347 noexcept(lhs.has_error(rhs.error()))
1348 ) -> bool {
1349 static_assert(is_equality_comparable_v<error_type>, "Error type must be equality comparable");
1350 if (lhs.has_value()) {
1351 return rhs.has_value();
1352 }
1353 return rhs.has_error(lhs.storage().unsafe_error());
1354 }
1355 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
1356 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
1357 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
1358
1359 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: This is a namespace scope function used in other TUs"
1360 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Conditionally noexcept based on underlying comparison"
1361 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
1362 /// @brief Compare two results for inequality.
1363 /// @param lhs The first result
1364 /// @param rhs The second result
1365 /// @return bool @c true if the both @c lhs and @c rhs hold values that are not equal, or both hold errors that are
1366 /// not equal, or one holds a value and the other holds an error, @c false otherwise
1367 ARENE_NODISCARD friend constexpr auto operator!=(result const& lhs, result const& rhs) noexcept(noexcept(lhs == rhs))
1368 -> bool {
1369 return !(lhs == rhs);
1370 }
1371 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
1372 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
1373 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
1374
1375 private:
1376 /// @brief Helper for implementing assignment which handles copy/move.
1377 /// @tparam OtherType the result type to assign from. @c OtherType::error_type must be nothrow constructible and
1378 /// nothrow
1379 /// assignable to @c error_type respectably.
1380 /// @param other the result to assign from.
1381 /// @post The state of @c this will be as follows:
1382 /// * If both @c this and @c other 's value channels are populated, it is a noop.
1383 /// * If both @c this and @c other 's error channels are populated, then @c this will have its error channel's
1384 /// state set as if via assignment from @c other 's error channel.
1385 /// * OtherTypewise, @c this will have its state set as if @c emplace 'd with the active channel of @c other .
1386 /// @return result& A reference to @c this post assignment.
1387 template <typename OtherType, constraints<std::enable_if_t<decays_to_v<OtherType, result>>> = nullptr>
1388 constexpr auto assign(OtherType&& other) noexcept -> result& {
1389 if (this->has_value()) {
1390 if (other.has_value()) {
1391 // assignment from two void values isn't meaningful, so nothing to do.
1392 return *this;
1393 }
1394 std::ignore = this->emplace(
1395 in_place_error,
1396 forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_error())
1397 );
1398 return *this;
1399 }
1400 if (other.has_error()) {
1401 this->storage().unsafe_error() = forward_like<OtherType>(std::forward<OtherType>(other).storage().unsafe_error());
1402 return *this;
1403 }
1404 // we switch ourselves to void.
1405 this->emplace(in_place_value);
1406 return *this;
1407 }
1408
1409 ///
1410 /// @brief deduced-this helper for @c result::and_then<F>(F&&) .
1411 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
1412 /// @tparam F Type of the visiting functor. Must be invocable no arguments, and return @c result<U,error_type> , where
1413 /// @c U may be any valid @c value_type .
1414 /// @param self the instance of the result to use.
1415 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
1416 /// @return result<U, error_type> which will be either the result of invoking @c handle_value , or it will contain a
1417 /// copy of @c self.error() .
1418 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
1419 static constexpr auto and_then(SelfType&& self, F&& handle_value) {
1420 using f_result_t = invoke_result_t<F>;
1421 static_assert(is_instantiation_of_v<f_result_t, ::arene::base::result>, "handle_value must return a result!");
1422 static_assert(
1423 std::is_same<typename f_result_t::error_type, typename remove_cvref_t<SelfType>::error_type>::value,
1424 "The result returned from handle_value's error channel must be the same as this result's error channel"
1425 );
1426 if (self.has_value()) {
1427 return ::arene::base::invoke(std::forward<F>(handle_value));
1428 }
1429 return f_result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
1430 }
1431
1432 ///
1433 /// @brief deduced-this helper for @c result::or_else<F>(F&&) .
1434 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
1435 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1436 /// of @c SelfType and return @c result<void,E> , where @c E may be any valid @c error_type .
1437 /// @param self the instance of the result to use.
1438 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1439 /// @return result<void, E> which will be either the result of invoking @c handle_error with the contents of
1440 /// @c self.error() , or it will have its value channel populated .
1441 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
1442 static constexpr auto or_else(SelfType&& self, F&& handle_error) {
1443 using f_result_t =
1444 invoke_result_t<F, decltype(forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error()))>;
1445 static_assert(is_instantiation_of_v<f_result_t, ::arene::base::result>, "handle_error must return a result!");
1446 static_assert(
1447 std::is_same<typename f_result_t::value_type, typename remove_cvref_t<SelfType>::value_type>::value,
1448 "The result returned from handle_error's value channel must be the same as this result's value channel"
1449 );
1450 if (self.has_error()) {
1451 return ::arene::base::invoke(
1452 std::forward<F>(handle_error),
1453 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())
1454 );
1455 }
1456 return f_result_t{};
1457 }
1458
1459 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
1460 /// @brief deduced-this helper for @c result::transform<F>(F&&) .
1461 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
1462 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1463 /// of @c SelfType , or with no arguments if @c value_type is @c void . May return any valid @c value_type
1464 /// @param self the instance of the result to use.
1465 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
1466 /// @return result<U, error_type> where @c U is the return type of @c F , which will be one of:
1467 /// * A @c result<U,error_type> constructed from the result of invoking @c handle_value if @c self.has_value()
1468 /// is @c true
1469 /// * A @c result<U,error_type> constructed from a copy of @c self.error() otherwise.
1470 template <
1471 typename SelfType,
1472 typename F,
1474 std::enable_if<decays_to_v<SelfType, result>>,
1476 static constexpr auto transform(SelfType&& self, F&& handle_value) {
1477 using f_value_t = invoke_result_t<F>;
1478 using result_t = result<f_value_t, error_type>;
1479 if (self.has_value()) {
1480 return result_t{in_place_value, ::arene::base::invoke(std::forward<F>(handle_value))};
1481 }
1482 return result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
1483 }
1484
1485 /// @brief deduced-this helper for @c result::transform<F>(F&&) .
1486 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
1487 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1488 /// of @c SelfType , or with no arguments if @c value_type is @c void . May return any valid @c value_type
1489 /// @param self the instance of the result to use.
1490 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
1491 /// @return result<void, error_type> which will be one of:
1492 /// * A default-constructed @c result<void,error_type> if @c self.has_value() is @c true
1493 /// * A @c result<void,error_type> constructed from a copy of @c self.error() otherwise.
1494 template <
1495 typename SelfType,
1496 typename F,
1498 std::enable_if<decays_to_v<SelfType, result>>,
1500 static constexpr auto transform(SelfType&& self, F&& handle_value) {
1501 using result_t = result<void, error_type>;
1502 if (self.has_value()) {
1504 return result_t{};
1505 }
1506 return result_t{in_place_error, forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())};
1507 }
1508 // parasoft-end-suppress AUTOSAR-A2_10_1-d
1509
1510 ///
1511 /// @brief deduced-this helper for @c result::transform_error<F>(F&&) .
1512 /// @tparam SelfType The qualified result type to invoke with. Must satisfy @c decays_to<SelfType,result>
1513 /// @tparam F Type of the visiting functor. Must be invocable with @c error_type matching the const/ref qualification
1514 /// of @c SelfType. May return any valid @c error_type .
1515 /// @param self the instance of the result to use.
1516 /// @param handle_error the functor to invoke if @c self.has_error() is @c true .
1517 /// @return result<value_type, E> which will be either:
1518 /// * A result constructed from the result of invoking @c handle_error with the contents of @c self.error() ,
1519 /// * A result constructed from a copy of @c self.value() .
1520 template <typename SelfType, typename F, constraints<std::enable_if<decays_to_v<SelfType, result>>> = nullptr>
1521 static constexpr auto transform_error(SelfType&& self, F&& handle_error) {
1522 using f_error_t =
1523 invoke_result_t<F, decltype(forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error()))>;
1524 using result_t = result<void, f_error_t>;
1525 if (self.has_error()) {
1526 return result_t{
1527 in_place_error,
1529 std::forward<F>(handle_error),
1530 forward_like<SelfType>(std::forward<SelfType>(self).storage().unsafe_error())
1531 )
1532 };
1533 }
1534 return result_t{};
1535 }
1536}; // result<void,E>
1537// parasoft-end-suppress AUTOSAR-A12_0_1-a-2
1538// parasoft-end-suppress AUTOSAR-M14_5_3-a-2
1539// parasoft-end-suppress AUTOSAR-A1_1_1-b-2
1540// parasoft-end-suppress AUTOSAR-A10_2_1-b-2
1541
1542// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1543/// @brief Visit the contained value or error with two independent functors.
1544/// @tparam ResultType The type of the @c result object to visit.
1545/// @tparam OnValue The type of the invocable to invoke if the @c result holds a value . Must satisfy @c is_invocable
1546/// with the result of @c self.value() , and must have the same return type as @c OnValue .
1547/// @tparam OnError The type of the invocable to invoke if the @c result holds an error. Must satisfy @c is_invocable
1548/// with the result of @c self.error() , and must have the same return type as @c OnValue .
1549/// @param self The @c result object
1550/// @param on_expected The invocable to invoke if @c self holds a value
1551/// @param on_unexpected The invocable to invoke if @c self holds an error
1552/// @return auto The result of invoking the appropriate invocable
1553///
1554/// Example Usage:
1555/// @snippet docs/examples/result_examples.cpp expand_result
1556template <
1557 class ResultType,
1558 class OnValue,
1559 class OnError,
1562 std::enable_if_t<std::is_same<
1563 invoke_result_t<OnValue, decltype(std::declval<ResultType>().value())>,
1564 invoke_result_t<OnError, decltype(std::declval<ResultType>().error())>>::value>> = nullptr>
1565constexpr auto expand_result(
1566 ResultType&& self,
1567 OnValue&& on_expected,
1568 OnError&& on_unexpected
1570 is_nothrow_invocable_v<OnError, decltype(std::declval<ResultType>().error())>) -> decltype(auto) {
1571 if (self.has_value()) {
1572 return ::arene::base::invoke(std::forward<OnValue>(on_expected), std::forward<ResultType>(self).value());
1573 }
1574 return ::arene::base::invoke(std::forward<OnError>(on_unexpected), std::forward<ResultType>(self).error());
1575}
1576// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1577
1578///
1579/// @brief Intermediary type for constructing a @c result with a given @c value_type .
1580/// @note Generally this should not be constructed manually; it intended as a transient type that allow to construct a
1581/// @c result Object while using type deduction for @c value_type and @c error_type .
1582/// @tparam ValueType the @c value_type of a @c result constructed from this type.
1583/// @see @c arene::base::value_result for the factory function that should be used in user code.
1584/// @{
1585template <typename ValueType>
1587 public:
1588 // parasoft-begin-suppress AUTOSAR-A13_5_2-a-2 "Implicit conversion to result is part of the API"
1589 // parasoft-begin-suppress AUTOSAR-A13_2_3-a-2 "False positive: Not a relational operator"
1590 /// @brief Instantiates a @c result<ValueType,Error> from the contained value instance. Consumes the container.
1591 /// @tparam Error the @c error_type of the produced @c result .
1592 template <typename Error>
1593 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
1594 constexpr operator result<ValueType, Error>(
1595 ) const& noexcept(noexcept(result<ValueType, Error>(in_place_value, std::declval<ValueType const&>()))) {
1596 return {in_place_value, value_};
1597 }
1598 /// @brief Instantiates a @c result<ValueType,Error> from the contained value instance. Consumes the container.
1599 /// @tparam Error the @c error_type of the produced @c result .
1600 template <typename Error>
1601 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
1602 constexpr operator result<ValueType, Error>(
1603 ) && noexcept(noexcept(result<ValueType, Error>(in_place_value, std::declval<ValueType&&>()))) {
1604 return {in_place_value, std::move(value_)};
1605 }
1606 // parasoft-end-suppress AUTOSAR-A13_2_3-a-2
1607 // parasoft-end-suppress AUTOSAR-A13_5_2-a-2
1608
1609 /// @brief Construct the stored value from the specified arguments
1610 /// @tparam ArgTypes the types of the arguments
1611 /// @param args The arguments
1612 template <
1613 typename... ArgTypes,
1615 nullptr>
1616 // NOLINTNEXTLINE(hicpp-explicit-conversions)
1617 constexpr value_result_t(ArgTypes&&... args) noexcept(std::is_nothrow_constructible<ValueType, ArgTypes...>::value)
1618 : value_(std::forward<ArgTypes>(args)...) {}
1619
1620 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constructor constrained via SFINAE is permitted"
1621 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
1622 /// @brief Copy constructor
1623 constexpr value_result_t(value_result_t const&) = default;
1624 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
1625
1626 /// @brief Move constructor
1627 constexpr value_result_t(value_result_t&&) = default;
1628
1629 /// @brief Copy assignment
1630 constexpr auto operator=(value_result_t const&) -> value_result_t& = default;
1631 /// @brief Move constructor
1632 constexpr auto operator=(value_result_t&&) -> value_result_t& = default;
1633 // parasoft-end-suppress CERT_C-EXP37-a-3
1634
1635 /// @brief destructor
1636 ~value_result_t() = default;
1637
1638 private:
1639 /// @brief The contained value
1640 ValueType value_;
1641};
1642
1643/// @brief Intermediary type for constructing a @c result with a @c void value
1644/// @note Generally this should not be constructed manually; it intended as a transient type that allow to construct a
1645/// @c result Object while using type deduction for @c value_type and @c error_type .
1646/// @see @c arene::base::value_result for the factory function that should be used in user code.
1647template <>
1648class value_result_t<void> {
1649 public:
1650 // parasoft-begin-suppress AUTOSAR-A13_5_2-a-2 "Implicit conversion to result is part of the API"
1651 // parasoft-begin-suppress AUTOSAR-A13_2_3-a-2 "False positive: Not a relational operator"
1652 /// @brief Instantiates a @c result<Value,Error> from the contained value instance. Consumes the container.
1653 /// @tparam Error the @c error_type of the produced @c result .
1654 template <typename Error>
1655 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
1656 constexpr operator result<void, Error>() noexcept {
1657 return {};
1658 }
1659 // parasoft-end-suppress AUTOSAR-A13_2_3-a-2
1660 // parasoft-end-suppress AUTOSAR-A13_5_2-a-2
1661};
1662/// @}
1663
1664///
1665/// @brief Intermediary type for constructing a @c result with a given @c error_type .
1666/// @note Generally this should not be constructed manually; it intended as a transient type that allow to construct a
1667/// @c result Object while using type deduction for @c value_type and @c error_type .
1668/// @tparam ErrorType the @c error_type of a @c result constructed from this type.
1669/// @see @c arene::base::error_result for the factory function that should be used in user code.
1670template <typename ErrorType>
1672 public:
1673 // parasoft-begin-suppress AUTOSAR-A13_5_2-a-2 "Implicit conversion to result is part of the API"
1674 // parasoft-begin-suppress AUTOSAR-A13_2_3-a-2 "False positive: Not a relational operator"
1675 /// @brief Instantiates a @c result<Value,ErrorType> from the contained error instance.
1676 /// @tparam Value the @c value_type of the produced @c result .
1677 template <typename Value>
1678 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
1679 constexpr operator result<Value, ErrorType>(
1680 ) const& noexcept(noexcept(result<Value, ErrorType>(in_place_error, std::declval<ErrorType const&>()))) {
1681 return {in_place_error, error_};
1682 }
1683 /// @brief Instantiates a @c result<Value,ErrorType> from the contained error instance, moving the error value into
1684 /// the @c result
1685 /// @tparam Value the @c value_type of the produced @c result .
1686 template <typename Value>
1687 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
1688 constexpr operator result<Value, ErrorType>(
1689 ) && noexcept(noexcept(result<Value, ErrorType>(in_place_error, std::declval<ErrorType&&>()))) {
1690 return {in_place_error, std::move(error_)};
1691 }
1692 // parasoft-end-suppress AUTOSAR-A13_2_3-a-2
1693 // parasoft-end-suppress AUTOSAR-A13_5_2-a-2
1694
1695 /// @brief Construct the stored error from the specified arguments
1696 /// @tparam ArgTypes the types of the arguments
1697 /// @param args The arguments
1698 template <
1699 typename... ArgTypes,
1701 nullptr>
1702 // NOLINTNEXTLINE(hicpp-explicit-conversions)
1703 constexpr error_result_t(ArgTypes&&... args) noexcept(std::is_nothrow_constructible<ErrorType, ArgTypes...>::value)
1704 : error_(std::forward<ArgTypes>(args)...) {}
1705
1706 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constructor constrained via SFINAE is permitted"
1707 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
1708 /// @brief Copy constructor
1709 constexpr error_result_t(error_result_t const&) = default;
1710 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
1711
1712 /// @brief Move constructor
1713 constexpr error_result_t(error_result_t&&) = default;
1714
1715 /// @brief Copy assignment
1716 constexpr auto operator=(error_result_t const&) -> error_result_t& = default;
1717 /// @brief Move constructor
1718 constexpr auto operator=(error_result_t&&) -> error_result_t& = default;
1719 // parasoft-end-suppress CERT_C-EXP37-a-3
1720
1721 /// @brief destructor
1722 ~error_result_t() = default;
1723
1724 private:
1725 /// @brief The contained error
1726 ErrorType error_;
1727};
1728
1729// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1730// parasoft-begin-suppress AUTOSAR-A2_10_1-a-2 "False positive: No global or namespace entity named 'value'"
1731///
1732/// @brief Convenience function to construct a @c result<ValueType,Error> containing the @c ValueType from any
1733/// reference.
1734///
1735/// Example Usage:
1736/// @snippet docs/examples/result_examples.cpp value_result
1737///
1738/// @tparam ValueType the @c value_type of the produced result.
1739/// @param value value to instantiate the @c result with.
1740/// @return value_result_t An object which is move-convertible to any @c result<ValueType,Error> .
1741template <typename ValueType>
1742constexpr auto value_result(ValueType&& value
1745 return {std::forward<ValueType>(value)};
1746}
1747// parasoft-end-suppress AUTOSAR-A2_10_1-a-2
1748// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1749
1750///
1751/// @brief Convenience function to construct a @c result<void,Error> .
1752///
1753/// Example Usage:
1754/// @snippet docs/examples/result_examples.cpp value_result_void
1755///
1756/// @return value_result_t An object which is move-convertible to any @c result<void,Error> .
1757inline constexpr auto value_result() noexcept -> value_result_t<void> { return {}; }
1758
1759// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
1760// parasoft-begin-suppress AUTOSAR-A2_10_1-a-2 "False positive: No global or namespace entity named 'error'"
1761/// @brief Convenience function to construct a @c result<Value,ErrorType> containing the @c ErrorType from any
1762/// reference.
1763///
1764/// Example Usage:
1765/// @snippet docs/examples/result_examples.cpp error_result
1766///
1767/// @tparam ErrorType the @c error_type of the produced result.
1768/// @param error error to instantiate the @c result with.
1769/// @return error_result_t An object which is move-convertible to any @c result<Value,ErrorType> .
1770template <typename ErrorType>
1771constexpr auto error_result(ErrorType&& error
1774 return {std::forward<ErrorType>(error)};
1775}
1776// parasoft-end-suppress AUTOSAR-A2_10_1-a-2
1777// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
1778
1779} // namespace base
1780} // namespace arene
1781
1782// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "False positive: specialization of standard templates is permitted"
1783// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "False positive: specialization of standard templates is permitted"
1784namespace std {
1785
1786// parasoft-begin-suppress AUTOSAR-A11_0_2-a-2 "False positive: must follow the primary template"
1787/// @brief Specialization of @c std::hash for @c arene::base::result
1788///
1789/// @tparam V the type of the value channel. @c std::hash<V> must be well-formed for this specialization to be
1790/// well-formed.
1791/// @tparam E the type of the error channel. @c std::hash<E> must be well-formed for this specialization to be
1792/// well-formed.
1793///
1794template <typename V, typename E>
1795struct hash<::arene::base::result<V, E>> {
1796 // parasoft-begin-suppress AUTOSAR-A18_1_6-a "False positive: The declaration does have a noexcept specifier"
1797 // parasoft-begin-suppress AUTOSAR-A2_10_1-a-2 "False positive: No global or namespace entity named 'value'"
1798 /// @brief Calculate the hash of a @c result
1799 /// @param value The instance of @c result to hash
1800 /// @return The result of hashing the stored value if @c value has a value, the result of hashing the stored error
1801 /// otherwise
1802 constexpr auto operator()(::arene::base::result<V, E> const& value) const
1803 noexcept(noexcept(hash<E>{}(std::declval<E const&>())) && noexcept(hash<V>{}(std::declval<V const&>())))
1804 -> std::size_t {
1805 return value.has_value() ? hash<V>{}(value.value()) : hash<E>{}(value.error());
1806 }
1807 // parasoft-end-suppress AUTOSAR-A2_10_1-a-2
1808 // parasoft-end-suppress AUTOSAR-A18_1_6-a-2
1809};
1810// parasoft-end-suppress AUTOSAR-A11_0_2-a-2
1811
1812// parasoft-begin-suppress AUTOSAR-A11_0_2-a-2 "False positive: must follow the primary template"
1813/// @brief Specialization of @c std::hash for @c arene::base::result<void, E>
1814///
1815/// @tparam E the type of the error channel. @c std::hash<E> must be well-formed for this specialization to be
1816/// well-formed.
1817///
1818template <typename E>
1819struct hash<::arene::base::result<void, E>> {
1820 // parasoft-begin-suppress AUTOSAR-A18_1_6-a "False positive: The declaration does have a noexcept specifier"
1821 // parasoft-begin-suppress AUTOSAR-A2_10_1-a-2 "False positive: No global or namespace entity named 'value'"
1822 /// @brief Calculate the hash of a @c result
1823 /// @param value The instance of @c result to hash
1824 /// @return The result of hashing @c monostate if @c value has a value, the result of hashing the stored error
1825 /// otherwise
1827 noexcept(noexcept(hash<E>{}(std::declval<E const&>()))) -> std::size_t {
1828 return value.has_value() ? hash<::arene::base::monostate>{}({}) : hash<E>{}(value.error());
1829 }
1830 // parasoft-end-suppress AUTOSAR-A2_10_1-a-2
1831 // parasoft-end-suppress AUTOSAR-A18_1_6-a-2
1832};
1833// parasoft-end-suppress AUTOSAR-A11_0_2-a-2
1834
1835} // namespace std
1836// parasoft-end-suppress CERT_CPP-DCL58-a-2
1837// parasoft-end-suppress AUTOSAR-A17_6_1-a-2
1838
1839#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RESULT_RESULT_HPP_
Intermediary type for constructing a result with a given error_type .
Definition result.hpp:1671
constexpr error_result_t(error_result_t &&)=default
Move constructor.
~error_result_t()=default
destructor
constexpr auto operator=(error_result_t &&) -> error_result_t &=default
Move constructor.
constexpr error_result_t(ArgTypes &&... args) noexcept(std::is_nothrow_constructible< ErrorType, ArgTypes... >::value)
Construct the stored error from the specified arguments.
Definition result.hpp:1703
constexpr auto operator=(error_result_t const &) -> error_result_t &=default
Copy assignment.
constexpr error_result_t(error_result_t const &)=default
Copy constructor.
constexpr auto transform(F &&handle_value) const &
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:1251
~result()=default
Destroy the stored value or error.
friend constexpr void swap(result &lhs, result &rhs) noexcept(noexcept(lhs.swap(rhs)))
Swaps the state of two results.
Definition result.hpp:1133
ARENE_NODISCARD friend constexpr auto operator==(result const &lhs, result const &rhs) noexcept(noexcept(lhs.has_error(rhs.error()))) -> bool
Compare two results for equality.
Definition result.hpp:1346
constexpr auto or_else(F &&handle_error) const &&
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:1223
constexpr result(in_place_value_t) noexcept(noexcept(base_type(in_place_value, monostate{})))
Constructs a result with the value-channel engaged.
Definition result.hpp:934
constexpr result()=default
Constructs a result with the value-channel engaged.
constexpr auto or_else(F &&handle_error) const &
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:1215
E error_type
The type when the result contains an error.
Definition result.hpp:902
ARENE_NODISCARD constexpr auto error() &&noexcept -> error_type &&
Accesses a reference to the content of the error channel.
Definition result.hpp:1067
constexpr auto transform(F &&handle_value) &&
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:1255
constexpr auto and_then(F &&handle_value) const &
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:1178
constexpr auto and_then(F &&handle_value) const &&
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:1186
constexpr auto operator=(move_assign_arg &&other) noexcept -> result &
Move-assignment for the case that error_type satisfies std::is_nothrow_move_constructible and std::is...
Definition result.hpp:1016
constexpr auto transform_error(F &&handle_error) &
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:1283
constexpr void value() const &noexcept
Access the content of the value channel.
Definition result.hpp:1043
constexpr void swap(result &other) noexcept(is_nothrow_swappable_v< SourceErrorType >)
Swaps the state of two results.
Definition result.hpp:1097
constexpr auto transform_error(F &&handle_error) &&
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:1317
constexpr auto operator=(copy_assign_arg const &other) noexcept -> result &
Copy-assignment for the case that error_type satisfies std::is_nothrow_copy_constructible and std::is...
Definition result.hpp:986
constexpr result(result &&move) noexcept(noexcept(base_type(std::declval< result && >())))=default
Default move constructor.
ARENE_NODISCARD constexpr auto error() const &&noexcept -> error_type const &&
Accesses a reference to the content of the error channel.
Definition result.hpp:1073
ARENE_NODISCARD constexpr auto error() const &noexcept -> error_type const &
Accesses a reference to the content of the error channel.
Definition result.hpp:1062
constexpr result(in_place_value_t, monostate) noexcept(noexcept(base_type(in_place_value, monostate{})))
Constructs a result with the value-channel engaged. Provided for backwards compatibility.
Definition result.hpp:940
constexpr auto transform_error(F &&handle_error) const &
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:1300
constexpr result(result const &copy) noexcept(noexcept(base_type(std::declval< result const & >())))=default
Default copy constructor.
constexpr auto or_else(F &&handle_error) &&
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:1219
ARENE_NODISCARD friend constexpr auto operator!=(result const &lhs, result const &rhs) noexcept(noexcept(lhs==rhs)) -> bool
Compare two results for inequality.
Definition result.hpp:1367
constexpr auto or_else(F &&handle_error) &
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:1211
constexpr auto and_then(F &&handle_value) &
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:1174
constexpr void value() &&noexcept
Access the content of the value channel.
Definition result.hpp:1047
constexpr auto operator=(deleted_move_assign_arg &&other) noexcept -> result &=delete
Deleted Move-assignment if error_type does not satisfy std::is_nothrow_move_constructible and std::is...
ARENE_NODISCARD constexpr auto error() &noexcept -> error_type &
Accesses a reference to the content of the error channel.
Definition result.hpp:1057
constexpr auto transform(F &&handle_value) const &&
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:1260
void value_type
The type when the result contains a value.
Definition result.hpp:898
constexpr auto operator=(deleted_copy_assign_arg const &other) noexcept -> result &=delete
Deleted Copy-assignment if error_type does not satisfy std::is_nothrow_copy_constructible and std::is...
constexpr void operator*() const &noexcept
Access the content of the value channel.
Definition result.hpp:1038
constexpr auto transform(F &&handle_value) &
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:1247
constexpr auto and_then(F &&handle_value) &&
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:1182
void emplace(in_place_value_t) noexcept
Emplaces the void value into the result.
Definition result.hpp:1146
constexpr auto transform_error(F &&handle_error) const &&
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:1334
constexpr result(in_place_error_t, P &&... args) noexcept(noexcept(base_type(in_place_error, std::forward< P >(args)...)))
Constructs a result containing the error_type by way of direct-non-list-initialization.
Definition result.hpp:956
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
constexpr auto and_then(F &&handle_value) &
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:531
~result()=default
Destroy the stored value or error.
ARENE_NODISCARD constexpr auto value() const &noexcept -> value_type const &
Accesses a reference to the content of the value channel.
Definition result.hpp:329
constexpr void swap(result &other) noexcept(is_nothrow_swappable_v< SourceValueType > &&is_nothrow_swappable_v< SourceErrorType >)
Swaps the state of two results.
Definition result.hpp:436
constexpr auto operator=(move_assign_arg &&other) noexcept -> result &
Move-assignment for the case that value_type and error_type satisfy std::is_nothrow_move_constructibl...
Definition result.hpp:242
constexpr result(result &&move) noexcept(noexcept(base_type(std::declval< result && >())))=default
Default move constructor.
constexpr auto and_then(F &&handle_value) const &
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:535
constexpr auto or_else(F &&handle_error) &
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:567
constexpr auto operator=(copy_assign_arg const &other) noexcept -> result &
Copy-assignment for the case that value_type and error_type satisfy std::is_nothrow_copy_constructibl...
Definition result.hpp:209
auto emplace(in_place_value_t, ArgTypes &&... args) noexcept -> value_type &
Emplaces a value into the result.
Definition result.hpp:501
constexpr auto transform(F &&handle_value) &
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:604
ARENE_NODISCARD constexpr auto value() const &&noexcept -> value_type const &&
Accesses a reference to the content of the value channel.
Definition result.hpp:350
constexpr auto transform(F &&handle_value) const &
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:608
ulp_distance_error error_type
Definition result.hpp:120
constexpr auto operator=(deleted_move_assign_arg &&other) noexcept -> result &=delete
Deleted Move-assignment if value_type or error_type do not satisfy std::is_nothrow_move_constructible...
ARENE_NODISCARD constexpr auto operator->() noexcept -> value_type *
Accesses a pointer to the content of the value channel.
Definition result.hpp:280
friend constexpr void swap(result &lhs, result &rhs) noexcept(noexcept(lhs.swap(rhs)))
Swaps the state of two results.
Definition result.hpp:480
ARENE_NODISCARD constexpr auto error() &&noexcept -> error_type &&
Accesses a reference to the content of the error channel.
Definition result.hpp:401
constexpr auto operator=(deleted_copy_assign_arg const &other) noexcept -> result &=delete
Deleted Copy-assignment if value_type or error_type do not satisfy std::is_nothrow_copy_constructible...
constexpr result(result const &copy) noexcept(noexcept(base_type(std::declval< result const & >())))=default
Default copy constructor.
constexpr auto transform(F &&handle_value) const &&
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:617
ARENE_NODISCARD constexpr auto operator*() &noexcept -> value_type &
Accesses a reference to the content of the value channel.
Definition result.hpp:292
constexpr auto transform_error(F &&handle_error) const &&
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:651
constexpr auto or_else(F &&handle_error) const &
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:571
constexpr auto transform_error(F &&handle_error) &
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:638
constexpr result(in_place_error_t, P &&... args) noexcept(noexcept(base_type(in_place_error, std::forward< P >(args)...)))
Constructs a result containing the error_type by way of direct-non-list-initialization.
Definition result.hpp:177
ARENE_NODISCARD constexpr auto operator->() const noexcept -> value_type const *
Accesses a pointer to the content of the value channel.
Definition result.hpp:286
constexpr auto or_else(F &&handle_error) &&
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:575
ARENE_NODISCARD friend constexpr auto operator!=(result const &lhs, result const &rhs) noexcept(noexcept(lhs==rhs)) -> bool
Compare two results for inequality.
Definition result.hpp:689
ARENE_NODISCARD constexpr auto operator*() const &&noexcept -> value_type const &&
Accesses a reference to the content of the value channel.
Definition result.hpp:311
ARENE_NODISCARD constexpr auto value_or(U &&default_value) const &noexcept -> value_type
Returns the value if the value channel is populated, else another value. Not declared if T is void .
Definition result.hpp:362
constexpr auto and_then(F &&handle_value) &&
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:539
ARENE_NODISCARD constexpr auto value_or(U &&default_value) &&noexcept -> value_type
Returns the value if the value channel is populated, else another value. Not declared if T is void .
Definition result.hpp:372
constexpr result()=default
Constructs a result with a default-constructed value_type . Does not participate in overload resoluti...
ARENE_NODISCARD constexpr auto operator*() const &noexcept -> value_type const &
Accesses a reference to the content of the value channel.
Definition result.hpp:298
constexpr auto transform(F &&handle_value) &&
Calls a provided functor with the value channel if it is populated, and produces a new result with th...
Definition result.hpp:612
ARENE_NODISCARD constexpr auto value() &noexcept -> value_type &
Definition result.hpp:319
ARENE_NODISCARD constexpr auto has_value(value_type const &val) const noexcept(noexcept(val==std::declval< value_type >())) -> bool
Checks if the value channel contains a specific value.
Definition result.hpp:262
ARENE_NODISCARD friend constexpr auto operator==(result const &lhs, result const &rhs) noexcept(noexcept(lhs.has_value(rhs.value())) &&noexcept(lhs.has_error(rhs.error()))) -> bool
Compare two results for equality.
Definition result.hpp:667
constexpr auto and_then(F &&handle_value) const &&
Monadic API which invokes a functor with the contents of value() if the value channel is populated.
Definition result.hpp:544
constexpr result(in_place_value_t, P &&... args) noexcept(noexcept(base_type(in_place_value, std::forward< P >(args)...)))
Constructs a result containing the value_type by way of direct-non-list-initialization.
Definition result.hpp:161
ARENE_NODISCARD constexpr auto operator*() &&noexcept -> value_type &&
Accesses a reference to the content of the value channel.
Definition result.hpp:304
constexpr auto or_else(F &&handle_error) const &&
Monadic API which invokes a functor with the contents of error() if the error channel is populated.
Definition result.hpp:580
ARENE_NODISCARD constexpr auto error() &noexcept -> error_type &
Accesses a reference to the content of the error channel.
Definition result.hpp:382
ARENE_NODISCARD constexpr auto value() &&noexcept -> value_type &&
Accesses a reference to the content of the value channel.
Definition result.hpp:339
ARENE_NODISCARD constexpr auto error() const &noexcept -> error_type const &
Accesses a reference to the content of the error channel.
Definition result.hpp:392
constexpr auto transform_error(F &&handle_error) const &
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:642
constexpr auto transform_error(F &&handle_error) &&
Calls a provided functor with the error channel if it is populated, and produces a new result with th...
Definition result.hpp:646
ARENE_NODISCARD constexpr auto error() const &&noexcept -> error_type const &&
Accesses a reference to the content of the error channel.
Definition result.hpp:411
ResultValue value_type
Definition result.hpp:116
Intermediary type for constructing a result with a given value_type .
Definition result.hpp:1586
constexpr auto operator=(value_result_t const &) -> value_result_t &=default
Copy assignment.
constexpr auto operator=(value_result_t &&) -> value_result_t &=default
Move constructor.
constexpr value_result_t(value_result_t &&)=default
Move constructor.
~value_result_t()=default
destructor
constexpr value_result_t(value_result_t const &)=default
Copy constructor.
constexpr value_result_t(ArgTypes &&... args) noexcept(std::is_nothrow_constructible< ValueType, ArgTypes... >::value)
Construct the stored value from the specified arguments.
Definition result.hpp:1617
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types.
Definition is_constructible.hpp:29
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
Trait to detect if the two template parameters are the same or not. The value member is true if the t...
Definition is_same.hpp:35
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Provides the decays_to_v<T, U> trait.
Implementation of a backport of std::forward_like.
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr bool is_nothrow_swappable_v
Trait query indicating if a type can be noexcept-swapped.
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
constexpr bool is_instantiation_of_v
Definition is_instantiation_of.hpp:16
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr auto error_result(ErrorType &&error) noexcept(std::is_nothrow_constructible< typename std::remove_reference< ErrorType >::type, ErrorType && >::value) -> error_result_t< std::remove_reference_t< ErrorType > >
Convenience function to construct a result<Value,ErrorType> containing the ErrorType from any referen...
Definition result.hpp:1771
constexpr auto invoke(F &&func, Args &&... args) noexcept(is_nothrow_invocable_v< F, Args... >) ->
Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments...
Definition invoke.hpp:281
constexpr bool has_overloaded_address_operator_v
Type trait to check if a type has an overloaded address operator; the value is true if the type has a...
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
constexpr auto forward_like
A backport of C++23's std::forward_like, which allows forwarding a type with the same reference and c...
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
constexpr auto value_result(ValueType &&value) noexcept(std::is_nothrow_constructible< typename std::remove_reference< ValueType >::type, ValueType && >::value) -> value_result_t< std::remove_reference_t< ValueType > >
Convenience function to construct a result<ValueType,Error> containing the ValueType from any referen...
Definition result.hpp:1742
constexpr auto expand_result(ResultType &&self, OnValue &&on_expected, OnError &&on_unexpected) noexcept(is_nothrow_invocable_v< OnValue, > &&is_nothrow_invocable_v< OnError, >) ->
Visit the contained value or error with two independent functors.
Definition result.hpp:1565
constexpr bool is_equality_comparable_v
Definition accumulate.hpp:36
STL namespace.
constexpr Type integral_constant< Type, Value >::value
Out-of-line definition for the value constant.
Definition integral_constant.hpp:64
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto addressof(T &value) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:24
typename remove_reference< T >::type remove_reference_t
An alias for T, but with top-level reference qualifiers removed.
Definition remove_reference.hpp:47
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26
A monostate type that is a well-behaved empty value for variants.
Definition monostate.hpp:28
constexpr auto operator()(::arene::base::result< V, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >())) &&noexcept(hash< V >{}(std::declval< V const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1802
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1826
hash function primary template
Definition hash.hpp:179
Provides the all_of_v<bool...> trait.