Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_OPTIONAL_OPTIONAL_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_OPTIONAL_OPTIONAL_HPP_
7
8// IWYU pragma: private
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
24#include "arene/base/optional/detail/optional_base.hpp"
25#include "arene/base/optional/detail/optional_value_interface.hpp" // IWYU pragma: keep
27#include "arene/base/scope_guard/scope_guard.hpp" // IWYU pragma: keep
28#include "arene/base/stdlib_choice/declval.hpp"
29#include "arene/base/stdlib_choice/enable_if.hpp"
30#include "arene/base/stdlib_choice/forward.hpp"
31#include "arene/base/stdlib_choice/is_assignable.hpp"
32#include "arene/base/stdlib_choice/is_constructible.hpp"
33#include "arene/base/stdlib_choice/is_convertible.hpp"
34#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
35#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
36#include "arene/base/stdlib_choice/is_move_assignable.hpp"
37#include "arene/base/stdlib_choice/is_move_constructible.hpp"
38#include "arene/base/stdlib_choice/is_same.hpp"
39#include "arene/base/stdlib_choice/move.hpp"
40#include "arene/base/stdlib_choice/remove_cv.hpp"
41#include "arene/base/stdlib_choice/remove_reference.hpp"
53// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
54
55// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
56// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
57
58namespace arene {
59namespace base {
60namespace detail {
61/// @brief Tag type used for @c arene::base::nullopt_t constructor
62struct unspecified_t {};
63} // namespace detail
64
65/// @brief Empty class type used to indicate that @c arene::base::optional does not contain a value.
66/// @note @c arene::base::nullopt_t is a non-aggregate type, has no default constructor, and no
67/// initializer-list constructor.
68class nullopt_t {
69 public:
70 /// @brief constructor for @c nullopt_t
71 /// @note This constructor exists to support both <c> op = {}; </c> and <c> op = nullopt; </c> as the syntax for
72 /// indicating that an @c arene::base::optional does not contain a value.
73 /// @note see ISO/IEC JTC1 SC22 WG21 N3672 for more nuanced discussion of the <c> op = {}; </c> syntax.
74 /// @note In arene-base version 3.47.1 and prior, this @c arene::base::nullopt_t was erroneously default
75 /// constructible.
76 /// @note The type of the parameter to this constructor is unspecified. Users should rely on @c arene::base::nullopt
77 /// for an instance of a @c arene::base::nullopt_t rather than constructing an instance of @c arene::base::nullopt_t
78 /// directly.
79 constexpr explicit nullopt_t(detail::unspecified_t) noexcept {}
80};
81
82/// @brief alias to maintain backwards compatibility with older versions of Arene Base.
83/// @see arene::base::nullopt_t
84/// @deprecated
86
87// parasoft-begin-suppress AUTOSAR-M3_4_1-b "This is a per-TU reference to a global used in multiple TUs"
88// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
89/// @brief Tag variable for empty-optional construction
91// parasoft-end-suppress CERT_CPP-DCL56-a
92// parasoft-end-suppress AUTOSAR-M3_4_1-b
93
94// parasoft-begin-suppress AUTOSAR-M3_4_1-b "This is a per-TU reference to a global used in multiple TUs"
95// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
96/// @brief alias to maintain backwards compatibility with older versions of Arene Base.
97/// @see arene::base::nullopt
98/// @deprecated
100// parasoft-end-suppress CERT_CPP-DCL56-a
101// parasoft-end-suppress AUTOSAR-M3_4_1-b
102
103/// @brief Implementation of @c std::optional for pre-C++17 compilers, and which replaces defined undefined behavior
104/// with @c ARENE_PRECONDITION guards.
105/// @tparam T The type which should be held in the optional.
106///
107/// An optional is a sum-type which can be in one of two states:
108/// 1. It is _disengaged_, which means it holds no value
109/// 2. It is _engaged_, which means it holds a value of type @c T .
110///
111/// It is generally used as a return type from an API which is _fallible_, but for which failure is an expected
112/// operating condition and for which no additional context other than a simple boolean pass/fail is useful. It can also
113/// be used as a member variable in a class to allow for deferred initialization without needing dynamic allocation, or
114/// as an input parameter to a function where it is important to distinguish between "not provided" and "default value."
115template <typename T>
116// parasoft-begin-suppress AUTOSAR-A10_1_1-a-2 "False positive: Only one non-interface base class"
117// NOLINTNEXTLINE(hicpp-special-member-functions,cppcoreguidelines-special-member-functions)
119 : optional_detail::optional_base<T>
122 , public optional_detail::optional_value_interface<optional<T>> {
123 // parasoft-end-suppress AUTOSAR-A10_1_1-a-2
124 /// @brief Internal type alias for the base class
125 using base_type = optional_detail::optional_base<T>;
126
127 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'storage_type' does not hide an identifier in
128 // 'optional_base'"
129 /// @brief Internal type alias for the storage type
130 using storage_type = typename base_type::storage_type;
131 // parasoft-end-suppress AUTOSAR-A2_10_1-e
132
133 /// @brief Internal type alias for the base class
135
136 /// @brief Internal type alias for the base class
137 using constructor_base = implicit_constructor_base<T>;
138
139 /// @brief Internal type alias for the base class
140 using interface_base = optional_detail::optional_value_interface<optional<T>>;
141
142 static_assert(!has_overloaded_address_operator_v<T>, "Stored type must not have an overloaded address operator");
143
144 public:
145 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'value_type' does not hide an identifier in
146 // 'ebo_holder'"
147 /// @brief The type stored in the optional
148 using value_type = T;
149 // parasoft-end-suppress AUTOSAR-A2_10_1-e
150
151 // parasoft-begin-suppress AUTOSAR-A12_7_1-a "Bases must be initialized explicitly to address a compiler bug"
152 /// @brief Default construct to an empty state
153 // Explicitly initializes bases rather than using @c =default to address a gcc 8 compiler bug
154 constexpr optional() noexcept
155 : base_type{},
156 constructor_base{},
157 compare_base{},
158 interface_base{} {}
159 // parasoft-end-suppress AUTOSAR-A12_7_1-a
160
161 /// @brief Default destructor
162 ~optional() = default;
163
164 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
165 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: Bases initialized via delegation"
166 /// @brief Explicitly construct to an empty state
167 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
168 constexpr optional(nullopt_t) noexcept
169 : optional{} {}
170 // parasoft-end-suppress AUTOSAR-A12_1_1-a
171 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
172
173 // parasoft-begin-suppress AUTOSAR-A12_8_6-a "Not guaranteed to be a base class"
174 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
175 /// @brief Copy constructor
177 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
178 // parasoft-end-suppress AUTOSAR-A12_8_6-a
179
180 /// @brief Move constructor
181 constexpr optional(optional&&) noexcept(
182 // NOLINTNEXTLINE(hicpp-noexcept-move)
183 std::is_nothrow_move_constructible<T>::value
184 ) = default;
185
186 /// @brief Construct from a value
187 /// @tparam U The type of the value to construct from
188 /// @param other The source to move from
189 template <
190 typename U,
192 std::enable_if_t<!std::is_same<optional, remove_cvref_t<U>>::value>,
193 std::enable_if_t<!std::is_same<nullopt_t, remove_cvref_t<U>>::value>,
194 std::enable_if_t<std::is_constructible<T, U&&>::value>> = nullptr>
195 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions,bugprone-forwarding-reference-overload)
196 constexpr optional(U&& other) noexcept(std::is_nothrow_constructible<T, U&&>::value)
197 : base_type(std::forward<U>(other)),
198 constructor_base{},
199 compare_base{} {}
200
201 /// @brief Construct from another optional
202 /// @tparam U The type of the value to construct from
203 /// @param other The source to move from
204 template <
205 typename U,
211 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
212 constexpr optional(optional<U>&& other) noexcept(noexcept(T(std::declval<U&&>())))
213 : base_type(std::move(other)),
214 constructor_base{},
215 compare_base{} {}
216
217 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
218 /// @brief Construct from another optional
219 /// @tparam U The type of the value to construct from
220 /// @param other The source to copy from
221 template <
222 typename U,
228 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
229 constexpr optional(optional<U> const& other) noexcept(noexcept(T(std::declval<U const&>())))
230 : base_type(other),
231 constructor_base{},
232 compare_base{} {}
233 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
234
235 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
236 /// @brief Construct from a set of arguments
237 /// @tparam Args The types of the arguments to construct the value from
238 /// @param args The arguments to construct the value from
239 template <
240 typename... Args,
242 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
243 constexpr optional(in_place_t, Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...)))
244 : base_type(in_place, std::forward<Args>(args)...),
245 constructor_base{},
246 compare_base{} {}
247 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
248
249 // parasoft-begin-suppress AUTOSAR-A12_8_6-a "Not guaranteed to be a base class"
250
251 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
252 /// @brief Reset via assignment
253 constexpr auto operator=(nullopt_t) noexcept -> optional& {
254 this->reset();
255 return *this;
256 }
257 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
258
259 // parasoft-begin-suppress AUTOSAR-A13_3_1-a-2 "False positive: Constrained via SFINAE"
260 /// @brief Copy assign
261 /// @param other The value to assign from
262 // NOLINTNEXTLINE(bugprone-exception-escape)
263 constexpr auto operator=(optional const& other
265 if (this != &other) {
266 internal_do_assign(other);
267 }
268 return *this;
269 }
270 // parasoft-end-suppress AUTOSAR-A13_3_1-a-2
271
272 // parasoft-begin-suppress AUTOSAR-A15_5_1-b-2 "False positive: Conditionally noxecept"
273 /// @brief Move assign
274 /// @param other The value to assign from
275 constexpr auto operator=(optional&& other)
276 // NOLINTNEXTLINE(bugprone-exception-escape, hicpp-noexcept-move) We want to exactly match T.
278 internal_do_assign(std::move(other));
279 return *this;
280 }
281 // parasoft-end-suppress AUTOSAR-A15_5_1-b-2
282
283 /// @brief Assign from a value
284 /// @param other The value to assign from
285 template <
286 typename U = T,
291 auto operator=(U&& other) noexcept(std::is_nothrow_assignable<T, U>::value&& noexcept(
292 std::declval<optional&>().construct_from(std::forward<U>(other))
293 )) -> optional& {
294 if (this->has_value()) {
295 **this = std::forward<U>(other);
296 } else {
297 construct_from(std::forward<U>(other));
298 }
299 return *this;
300 }
301
302 // parasoft-end-suppress AUTOSAR-A12_8_6-a
303
304 // Make has_value public from base class
305 using base_type::has_value;
306
307 /// @brief Query if this object has a value
308 ///
309 /// @return bool Equivalent to @c has_value() .
310 constexpr explicit operator bool() const noexcept { return this->has_value(); }
311
312 // parasoft-begin-suppress AUTOSAR-A9_3_1-a-2 "This is a wrapper, so non-const access to the value is required"
313 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
314 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
315 /// @brief Accesses the value in the optional.
316 ///
317 /// @return A @c T which is cref-qualified the same as @c *this .
318 /// @pre @c has_value() , else @c ARENE_PRECONDITION violation.
319 /// @{
320 constexpr auto operator*() const& noexcept -> T const& {
321 ARENE_PRECONDITION(this->has_value());
322 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
323 return this->storage().value;
324 }
325 constexpr auto operator*() & noexcept -> T& {
326 ARENE_PRECONDITION(this->has_value());
327 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
328 return this->storage().value;
329 }
330 constexpr auto operator*() const&& noexcept -> T const&& {
331 ARENE_PRECONDITION(this->has_value());
332 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
333 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
334 return std::move(this->storage().value);
335 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
336 }
337 constexpr auto operator*() && noexcept -> T&& {
338 ARENE_PRECONDITION(this->has_value());
339 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
340 return std::move(this->storage().value);
341 }
342 /// @}
343 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
344 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
345 // parasoft-end-suppress AUTOSAR-A9_3_1-a-2
346
347 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
348 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
349 /// @brief Access a pointer to the value in the optional
350 ///
351 /// @return A @c T* which is const-qualified the same as @c *this .
352 /// @pre @c has_value() , else @c ARENE_PRECONDITION violation.
353 /// @{
354 constexpr auto operator->() const noexcept -> T const* {
355 ARENE_PRECONDITION(this->has_value());
356 return &**this;
357 }
358 constexpr auto operator->() noexcept -> T* {
359 ARENE_PRECONDITION(this->has_value());
360 return &**this;
361 }
362 /// @}
363 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
364 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
365
366 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
367 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
368 /// @brief Gets the value held in the optional, or else the provided default.
369 /// @tparam U The type of the default value. @c T must be constructible from @c U.
370 /// @param default_value The value to return if the optional is null.
371 /// @return T constructed from the held value if @c has_value() is @c true , else an instance constructed from
372 /// @c default_value .
373 /// @note This must return a new instance rather than a reference, otherwise it risks dangling references if
374 /// @c default_value is a temporary.
375 ///@{
376 template <
377 typename U,
378 typename R = T,
382 constexpr auto value_or(U&& default_value
383 ) const& noexcept(noexcept(optional::value_or(std::declval<optional const&>(), std::forward<U>(default_value))))
384 -> T {
385 return optional::value_or(*this, std::forward<U>(default_value));
386 }
387 template <
388 typename U,
389 typename R = T,
393 constexpr auto value_or(U&& default_value
394 ) && noexcept(noexcept(optional::value_or(std::declval<optional&&>(), std::forward<U>(default_value)))) -> T {
395 return optional::value_or(std::move(*this), std::forward<U>(default_value));
396 }
397 ///@}
398 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
399 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
400
401 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
402 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
403 /// @brief Gets the value held in the optional, or else invokes the provided callable to generate one.
404 /// @tparam F The type of the callable to invoke. Must satisfy @c is_invocable_v<F> and
405 /// @c std::is_convertible<invoke_result_t<F>,T> .
406 /// @param default_generator The function to invoke to produce a default if @c !has_value() .
407 /// @return T constructed from the held value if @c has_value() is @c true , else an instance constructed from
408 /// invoking @c default_generator .
409 ///@{
410 template <
411 typename F,
412 typename R = T,
416 constexpr auto value_or_else(F&& default_generator) const& noexcept(
418 ) -> T {
419 return optional::value_or_else(*this, std::forward<F>(default_generator));
420 }
421 template <
422 typename F,
423 typename R = T,
427 constexpr auto value_or_else(F&& default_generator
428 ) && noexcept(noexcept(optional::value_or_else(std::declval<optional&&>(), std::forward<F>(default_generator))))
429 -> T {
430 return optional::value_or_else(std::move(*this), std::forward<F>(default_generator));
431 }
432 ///@}
433 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
434 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
435
436 /// @brief Destroy the old value if there is one, and construct a new value
437 /// @tparam Args The types of the arguments for constructing the new value
438 /// @param args The arguments for constructing the new value
439 template <typename... Args>
440 constexpr void emplace(Args&&... args
441 ) noexcept(noexcept(std::declval<optional<T>>().construct_from(std::forward<Args>(args)...))) {
442 this->reset();
443 construct_from(std::forward<Args>(args)...);
444 }
445
446 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
447 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
448 /// @brief Monadic API which invokes a functor with the contents of @c value() if @c has_value() is @c true.
449 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
450 /// of @c Self, and return any valid <c>optional&lt;U&gt;</c>.
451 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
452 /// @return U where @c U is the return type of @c F and is an instantiation of @c optional , which will be:
453 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c true.
454 /// * A @c U instantiated with @c nullopt otherwise.
455 ///@{
456 template <typename F>
457 constexpr auto and_then(F&& handle_value) & -> invoke_result_t<F, T&> {
458 static_assert(is_invocable_v<F, T&>, "handle_value must be invocable with value_type.");
459 static_assert(
461 "handle_value must return an optional"
462 );
463 return optional::and_then(*this, std::forward<F>(handle_value));
464 }
465 template <typename F>
466 constexpr auto and_then(F&& handle_value) const& -> invoke_result_t<F, T const&> {
467 static_assert(is_invocable_v<F, T const&>, "handle_value must be invocable with value_type.");
468 static_assert(
470 "handle_value must return an optional"
471 );
472 return optional::and_then(*this, std::forward<F>(handle_value));
473 }
474 template <typename F>
475 constexpr auto and_then(F&& handle_value) && -> invoke_result_t<F, T&&> {
476 static_assert(is_invocable_v<F, T&&>, "handle_value must be invocable with value_type.");
477 static_assert(
479 "handle_value must return an optional"
480 );
481 return optional::and_then(std::move(*this), std::forward<F>(handle_value));
482 }
483 template <typename F>
484 constexpr auto and_then(F&& handle_value) const&& -> invoke_result_t<F, T const&&> {
485 static_assert(is_invocable_v<F, T const&&>, "handle_value must be invocable with value_type.");
486 static_assert(
488 "handle_value must return an optional"
489 );
490 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
491 return optional::and_then(std::move(*this), std::forward<F>(handle_value));
492 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
493 }
494 ///@}
495 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
496 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
497
498 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
499 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
500 /// @brief Monadic API which invokes a functor if @c has_value() is @c false.
501 /// @tparam F Type of the visiting functor. Must be invocable with no arguments and return @c optional<T> .
502 /// @param handle_null the functor to invoke if @c self.has_value() is @c false .
503 /// @return optional<T> which will be:
504 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c false.
505 /// * A instance of @c optional<T> constructed from forwarding @c self .
506 ///@{
507 template <
508 typename F,
509 typename R = T,
511 nullptr>
512 constexpr auto or_else(F&& handle_null) const& -> optional<T> {
513 static_assert(
515 "handle_null must be void invocable and return an optional with the same type as this."
516 );
517 return optional::or_else(*this, std::forward<F>(handle_null));
518 }
519 template <
520 typename F,
521 typename R = T,
523 nullptr>
524 constexpr auto or_else(F&& handle_null) && -> optional<T> {
525 static_assert(
527 "handle_null must be void invocable and return an optional with the same type as this."
528 );
529 return optional::or_else(std::move(*this), std::forward<F>(handle_null));
530 }
531 ///@}
532 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
533 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
534
535 // parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: All overloads are fully are documented"
536 // parasoft-begin-suppress AUTOSAR-A2_7_3-b-2 "False positive: All overloads are fully are documented"
537 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
538 /// @brief Calls a provided functor with @c value if it is populated, and produces a new @c optional with the return
539 /// from it.
540 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
541 /// of @c this. May return any valid @c value_type
542 /// @param value_transformer the functor to invoke if @c self.has_value() is @c true .
543 /// @return <c>optional&lt;U&gt;</c> where @c U is the return type of @c F , which will contain:
544 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c true.
545 /// * @c nullopt otherwise.
546 ///@{
547 template <typename F>
548 constexpr auto transform(F&& value_transformer) & -> optional<invoke_result_t<F, T&>> {
549 static_assert(is_invocable_v<F, T&>, "value_transformer must be invocable with value_type.");
550 static_assert(
552 "value_transformer must return a type that can be validly placed in an optional"
553 );
554 return optional::transform(*this, std::forward<F>(value_transformer));
555 }
556 template <typename F>
557 constexpr auto transform(F&& value_transformer) const& -> optional<invoke_result_t<F, T const&>> {
558 static_assert(is_invocable_v<F, T const&>, "value_transformer must be invocable with value_type.");
559 static_assert(
561 "value_transformer must return a type that can be validly placed in an optional"
562 );
563 return optional::transform(*this, std::forward<F>(value_transformer));
564 }
565 template <typename F>
566 constexpr auto transform(F&& value_transformer) && -> optional<invoke_result_t<F, T&&>> {
567 static_assert(is_invocable_v<F, T&&>, "value_transformer must be invocable with value_type.");
568 static_assert(
570 "value_transformer must return a type that can be validly placed in an optional"
571 );
572 return optional::transform(std::move(*this), std::forward<F>(value_transformer));
573 }
574 template <typename F>
575 constexpr auto transform(F&& value_transformer) const&& -> optional<invoke_result_t<F, T const&&>> {
576 static_assert(is_invocable_v<F, T const&&>, "value_transformer must be invocable with value_type.");
577 static_assert(
579 "value_transformer must return a type that can be validly placed in an optional"
580 );
581 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "We want to ensure the qualifier is preserved"
582 return optional::transform(std::move(*this), std::forward<F>(value_transformer));
583 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2
584 }
585 ///@}
586 // parasoft-end-suppress AUTOSAR-A2_7_3-b-2
587 // parasoft-end-suppress AUTOSAR-A2_7_3-a-2
588 // parasoft-end-suppress AUTOSAR-A2_10_1-d
589
590 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False Positive: 'swap' does not hide anything"
591 ///
592 /// @brief Exchanges the state of two optionals.
593 ///
594 /// @tparam U Must satisfy @c is_swappable_v and @c std::is_move_constructable.
595 /// @param other The optional to swap with.
596 /// @post If both optionals are engaged, they will have their values exchanged as if via @c arene::base::swap .
597 /// Otherwise if one of the optionals was @c nullopt , the states are exchanged as if by move-constructing the
598 /// value from the engaged optional into the unengaged optional, and resetting the previously engaged optional.
599 ///
600 template <
601 typename U = T,
603 nullptr>
604 constexpr void swap(optional& other) noexcept( //
606 ) {
607 if (this->has_value() && other.has_value()) {
608 ::arene::base::swap(**this, *other);
609 } else if (this->has_value()) {
610 other.construct_from(std::move(**this));
611 this->reset();
612 } else if (other.has_value()) {
613 construct_from(std::move(*other));
614 other.reset();
615 } else {
616 // They're both empty, so do nothing. (This else block is required by AUTOSAR-M6_4_2)
617 }
618 }
619 // parasoft-end-suppress AUTOSAR-A2_10_1-d
620
621 // parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code"
622 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: cannot apply static here"
623 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "Hidden friends permitted by A11-3-1 Permit #2"
624 /// @brief swaps the elements between two optionals.
625 ///
626 /// @tparam U the type of the element in the optional. Must satisfy @c is_swappable_v .
627 /// @param lhs the left hand optional to swap.
628 /// @param rhs the right hand optional to swap.
629 /// @post Equivalent to having called @c lhs.swap(rhs);
630 /// @see optional::swap.
631 ///
632 template <
633 typename U = T,
635 nullptr>
636 friend constexpr void swap(optional& lhs, optional& rhs) noexcept(noexcept(lhs.swap(rhs))) {
637 lhs.swap(rhs);
638 }
639 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
640 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
641 // parasoft-end-suppress AUTOSAR-A2_7_2-a
642
643 /// @brief Make @c reset public from base class
644 using base_type::reset;
645
646 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'three_way_compare' does not hide an identifier in
647 // 'optional'"
648 /// @brief Three-way comparison between an @c optional and @c nullopt_t
649 /// @param lhs The first object to compare
650 /// @return strong_ordering @c strong_ordering::equal if the @c lhs is empty, otherwise @c strong_ordering::greater
651 static constexpr auto three_way_compare(optional const& lhs, nullopt_t) noexcept -> strong_ordering {
652 return lhs ? strong_ordering::greater : strong_ordering::equal;
653 }
654 // parasoft-end-suppress AUTOSAR-A2_10_1-e
655
656 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'fast_inequality_check' does not hide an identifier in
657 // 'optional'"
658 /// @brief Fast inequality check between two @c optional instances, where there is not a fast inequality check between
659 /// the two stored types.
660 /// @tparam U The value type of the second @c optional
661 /// @param lhs The first object to compare
662 /// @param rhs The first object to compare
663 /// @return inequality_heuristic @c inequality_heuristic::definitely_not_equal if @c lhs has a value and @c rhs does
664 /// not, or vice versa, @c inequality_heuristic::may_or_may_not_be_equal otherwise
665 template <typename U, constraints<std::enable_if_t<!has_fast_inequality_check_v<T, U>>> = nullptr>
666 static constexpr auto fast_inequality_check(optional const& lhs, optional<U> const& rhs) noexcept
668 return (lhs.has_value() != rhs.has_value()) ? inequality_heuristic::definitely_not_equal
670 }
671 // parasoft-end-suppress AUTOSAR-A2_10_1-e
672
673 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'fast_inequality_check' does not hide an identifier in
674 // 'optional'"
675 /// @brief Fast inequality check between two @c optional instances, where there is not a fast inequality check between
676 /// the two stored types.
677 /// @tparam U The value type of the second @c optional
678 /// @param lhs The first object to compare
679 /// @param rhs The first object to compare
680 /// @return inequality_heuristic @c inequality_heuristic::definitely_not_equal if @c lhs has a value and @c rhs does
681 /// not, or vice versa, @c inequality_heuristic::may_or_may_not_be_equal if neither has a value, and delegeates to @c
682 /// fast_inequality_check on the stored values otherwise
683 template <typename U, constraints<std::enable_if_t<has_fast_inequality_check_v<T, U>>> = nullptr>
684 static constexpr auto fast_inequality_check(optional const& lhs, optional<U> const& rhs) noexcept
686 if (lhs.has_value() != rhs.has_value()) {
688 }
689 if (!lhs) {
691 }
692 return T::fast_inequality_check(*lhs, *rhs);
693 }
694 // parasoft-end-suppress AUTOSAR-A2_10_1-e
695
696 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'three_way_compare' does not hide an identifier in
697 // 'optional'"
698 /// @brief Three-way comparison between two @c optional instances
699 /// @tparam Self The type of the optional
700 /// @tparam U The value type of the second @c optional
701 /// @param lhs The first object to compare
702 /// @param rhs The first object to compare
703 /// @return strong_ordering @c strong_ordering::less if @c lhs is empty and @c rhs has a value, @c
704 /// strong_ordering::equal if both @c lhs and @c rhs are empty, @c strong_ordering::greater if @c lhs has a value and
705 /// @c rhs is empty, and the result of three-way comparison between the stored values if both @c lhs and @c rhs have
706 /// values
707 /// @pre @c T and @c U must be three-way-comparable
708 template <
709 typename Self,
710 typename U,
714 static constexpr auto three_way_compare(Self const& lhs, optional<U> const& rhs) noexcept -> strong_ordering {
715 if (lhs) {
716 if (rhs) {
717 return compare_three_way{}(*lhs, *rhs);
718 }
719 return strong_ordering::greater;
720 }
721 if (rhs) {
722 return strong_ordering::less;
723 }
724 return strong_ordering::equal;
725 }
726 // parasoft-end-suppress AUTOSAR-A2_10_1-e
727
728 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'fast_inequality_check' does not hide an identifier in
729 // 'optional'"
730 /// @brief Fast inequality check between an @c optional instance and a value, where there is not a fast inequality
731 /// check between the two stored types.
732 /// @tparam U The value type of the second @c optional
733 /// @param lhs The first object to compare
734 /// @return inequality_heuristic @c inequality_heuristic::definitely_not_equal if @c lhs is empty, @c
735 /// inequality_heuristic::may_or_may_not_be_equal otherwise
736 template <
737 typename U,
744 // parasoft-end-suppress AUTOSAR-A2_10_1-e
745
746 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'fast_inequality_check' does not hide an identifier in
747 // 'optional'"
748 /// @brief Fast inequality check between an @c optional instance and a value, where there is a fast inequality
749 /// check between the stored type and the other value.
750 /// @tparam U The value type of the second @c optional
751 /// @param lhs The first object to compare
752 /// @param rhs The first object to compare
753 /// @return inequality_heuristic @c inequality_heuristic::definitely_not_equal if @c lhs is empty, delegeates to @c
754 /// fast_inequality_check on the stored values otherwise
755 template <
756 typename U,
760 static constexpr auto fast_inequality_check(optional const& lhs, U const& rhs) noexcept -> inequality_heuristic {
761 if (!lhs) {
763 }
764 return T::fast_inequality_check(*lhs, rhs);
765 }
766 // parasoft-end-suppress AUTOSAR-A2_10_1-e
767
768 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'three_way_compare' does not hide an identifier in
769 // 'optional'"
770 /// @brief Three-way comparison between an @c optional instance and a value
771 /// @tparam Self The type of the optional
772 /// @tparam U the type of the value to compare against
773 /// @param lhs The first object to compare
774 /// @param rhs The first object to compare
775 /// @return strong_ordering @c strong_ordering::less if @c lhs is empty, and the result of three-way comparison
776 /// between the stored value of @c lhs and @c rhs otherwise
777 /// @pre @c T and @c U must be three-way-comparable
778 template <
779 typename Self,
780 typename U,
785 static constexpr auto three_way_compare(Self const& lhs, U const& rhs) noexcept -> strong_ordering {
786 if (lhs) {
787 return compare_three_way{}(*lhs, rhs);
788 }
789 return strong_ordering::less;
790 }
791 // parasoft-end-suppress AUTOSAR-A2_10_1-e
792
794 ARENE_IGNORE_ALL("-Wfloat-equal", "Can only suppress this here, not in user code; std::vector does the same thing");
795 ARENE_IGNORE_ALL("-Wsign-compare", "Can only suppress this here, not in user code; std::vector does the same thing");
796
797 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Mixed comparisons permitted by A13-5-5 Permit #1"
798 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
799 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: cannot apply static here"
800 /// @brief equality comparison between an @c optional instance and a value
801 /// @tparam Self The type of the optional
802 /// @tparam U the type of the value to compare against the optional
803 /// @param lhs The first object to compare
804 /// @param rhs The first object to compare
805 /// @return bool @c false if @c lhs is empty, and the result of an equality comparison
806 /// between the stored value of @c lhs and @c rhs otherwise
807 /// @pre @c T and @c U must be equality-comparable
808 template <
809 typename Self,
810 typename U,
815 friend constexpr auto operator==(Self const& lhs, U const& rhs) noexcept -> bool {
816 if (lhs) {
817 // parasoft-begin-suppress AUTOSAR-M5_0_4-a "The type being compared is up to the user"
818 // parasoft-begin-suppress AUTOSAR-M6_2_2-a "The type being compared is up to the user"
819 return *lhs == rhs;
820 // parasoft-end-suppress AUTOSAR-M6_2_2-a
821 // parasoft-end-suppress AUTOSAR-M5_0_4-a
822 }
823 return false;
824 }
825 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
826 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
827 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
828
830
831 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
832 // parasoft-begin-suppress AUTOSAR-A13_5_5-b-2 "Mixed comparisons permitted by A13-5-5 Permit #1"
833 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: cannot apply static here"
834 /// @brief equality comparison between two @c optional instances
835 /// @tparam Self The type of the optional
836 /// @tparam U the type stored in the other optional
837 /// @param lhs The first object to compare
838 /// @param rhs The first object to compare
839 /// @return bool @c true if both @c lhs and @c rhs are empty, @c false if only one of @c lhs and @c rhs is empty, and
840 /// the result of an equality comparison between the stored value of @c lhs and the stored value of @c rhs otherwise
841 /// @pre @c T and @c U must be equality-comparable
842 template <
843 typename Self,
844 typename U,
849 friend constexpr auto operator==(Self const& lhs, optional<U> const& rhs) noexcept -> bool {
850 if (lhs) {
851 if (rhs) {
852 return *lhs == *rhs;
853 }
854 return false;
855 }
856 if (rhs) {
857 return false;
858 }
859 return true;
860 }
861 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
862
863 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: these are public functions with external linkage"
864 /// @brief equality comparison between an @c optional instance and an explicit @c nullopt
865 /// @param lhs The @c optional instance to compare
866 /// @return bool @c true if @c lhs is null (that is, !has_value()), @c false if it has a value
867 friend constexpr auto operator==(optional const& lhs, nullopt_t) noexcept -> bool { return !lhs.has_value(); }
868
869 /// @brief inequality comparison between an @c optional instance and an explicit @c nullopt
870 /// @param lhs The @c optional instance to compare
871 /// @return bool @c true if @c lhs has a value (that is, has_value()), @c false if it does not
872 friend constexpr auto operator!=(optional const& lhs, nullopt_t) noexcept -> bool { return lhs.has_value(); }
873
874 /// @brief inequality comparison between an explicit @c nullopt and an @c optional instance
875 /// @param rhs The @c optional instance to compare
876 /// @return bool @c true if @c rhs has a value (that is, has_value()), @c false if it does not
877 friend constexpr auto operator!=(nullopt_t, optional const& rhs) noexcept -> bool { return rhs.has_value(); }
878 // parasoft-end-suppress AUTOSAR-A0_1_3-a
879
880 // parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: cannot apply static here"
881 /// @brief inequality comparison between two @c optional instances
882 /// @tparam Self The type of the optional
883 /// @tparam U the type stored in the other optional
884 /// @param lhs The first object to compare
885 /// @param rhs The first object to compare
886 /// @return bool @c false if @c lhs is empty, and the result of an inequality comparison
887 /// between the stored value of @c lhs and @c rhs otherwise
888 /// @pre @c T and @c U must be equality-comparable
889 template <
890 typename Self,
891 typename U,
896 friend constexpr auto operator!=(Self const& lhs, optional<U> const& rhs) noexcept -> bool {
897 return !(lhs == rhs);
898 }
899 // parasoft-end-suppress AUTOSAR-M3_3_2-a-2
900 // parasoft-end-suppress AUTOSAR-A13_5_5-b-2
901 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
902
903 private:
904 /// @brief Internal function to construct a new @c T in the storage.
905 /// This overload handles the case where everything is @c noexcept
906 /// @tparam Args The types of the arguments to construct from
907 /// @param args The arguments to construct from
908 /// Assumes @c has_value() is @c false
909 template <
910 typename... Args,
912 void construct_from(Args&&... args) noexcept {
913 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
914 this->storage().~storage_type();
915 construct_at(&this->storage(), in_place, std::forward<Args>(args)...);
916 this->set_has_value();
917 }
918
919 /// @brief Internal function to construct a new @c T in the storage.
920 /// This overload handles the case where the constructor might throw
921 /// @tparam Args The types of the arguments to construct from
922 /// @param args The arguments to construct from
923 /// Assumes @c has_value() is @c false
924 template <
925 typename... Args,
927 void construct_from(Args&&... args) {
928 // parasoft-begin-suppress AUTOSAR-A7_1_7-a "False positive: no expression statement and identifier declaration on
929 // same line"
930
931 // If the constructor throws, we want to leave the optional in a valid state while letting the exception bubble
932 // without using try/catch/throw in order to compile cleanly with -fno-exceptions. Therefore, we use a scope_guard
933 // which resets the storage, and cancel it if construction is successful.
934 auto make_empty_on_throw = on_scope_exit([this]() noexcept { construct_at(&this->storage()); });
935
936 // parasoft-end-suppress AUTOSAR-A7_1_7-a
937
938 construct_at(&this->storage(), in_place, std::forward<Args>(args)...);
939 this->set_has_value();
940 make_empty_on_throw.cancel();
941 }
942
943 /// @brief Internal function to implement assignment.
944 /// @tparam OtherOptional The type of the optional being assigned from, including the value category
945 /// @param other The optional being assigned from
946 template <typename OtherOptional>
947 constexpr void internal_do_assign(OtherOptional&& other) noexcept(
948 std::is_nothrow_constructible<T, decltype(*std::forward<OtherOptional>(other))>::value &&
949 std::is_nothrow_assignable<T&, decltype(*std::forward<OtherOptional>(other))>::value
950 ) {
951 if (this->has_value() && other.has_value()) {
952 **this = *std::forward<OtherOptional>(other);
953 } else if (this->has_value()) {
954 this->reset();
955 } else if (other.has_value()) {
956 construct_from(*std::forward<OtherOptional>(other));
957 } else {
958 // do nothing
959 }
960 }
961
962 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "Preserve type safety by allowing different instantiations of optional
963 // to access private members without making them public"
964 template <typename U>
965 friend class optional;
966 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
967
968 /// @brief deduced-this helper for @c optional::value_or<F>(F&&) .
969 /// @tparam Self The qualified result type to invoke with. Must satisfy @c decays_to<Self,optional>
970 /// @tparam U The type of the default value. @c T must be constructible from @c U.
971 /// @param self The @c optional to operate on
972 /// @param default_value The value to return if the optional is null.
973 /// @return T constructed from the held value if @c has_value() is @c true , else an instance constructed from
974 /// @c default_value .
975 /// @note This must return a new instance rather than a reference, otherwise it risks dangling references if
976 /// @c default_value is a temporary.
977 template <typename Self, typename U, constraints<std::enable_if_t<decays_to_v<Self, optional>>> = nullptr>
978 static constexpr auto value_or(Self&& self, U&& default_value) noexcept(noexcept(T{default_value}
979 ) && noexcept(T{*std::forward<Self>(self)})) -> T {
980 return self.has_value() ? static_cast<T>(forward_like<Self>(std::forward<Self>(self).storage().value))
981 : static_cast<T>(std::forward<U>(default_value));
982 }
983
984 /// @brief deduced-this helper for @c optional::value_or_else<F>(F&&) .
985 /// @tparam Self The qualified result type to invoke with. Must satisfy @c decays_to<Self,optional>
986 /// @tparam F The type of the callable to invoke. Must have signature @c U(void) , where @c U is a type from which
987 /// @c T is constructible.
988 /// @param self the instance of the result to use.
989 /// @param default_generator The function to invoke to produce a default if optional is null.
990 /// @return T constructed from the held value if @c has_value() is @c true , else an instance constructed from
991 /// invoking @c default_generator .
992 template <typename Self, typename F, constraints<std::enable_if_t<decays_to_v<Self, optional>>> = nullptr>
993 static constexpr auto value_or_else(Self&& self, F&& default_generator) noexcept(noexcept(T{
994 ::arene::base::invoke(std::forward<F>(default_generator))
995 }) && noexcept(T{*std::forward<Self>(self)})) -> T {
996 return self.has_value() ? static_cast<T>(forward_like<Self>(std::forward<Self>(self).storage().value))
997 : static_cast<T>(::arene::base::invoke(std::forward<F>(default_generator)));
998 }
999
1000 ///
1001 /// @brief deduced-this helper for @c optional::and_then<F>(F&&) .
1002 /// @tparam Self The qualified result type to invoke with. Must satisfy @c decays_to<Self,optional>
1003 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1004 /// of @c Self, and return any valid <c>optional&lt;U&gt;</c>.
1005 /// @param self the instance of the result to use.
1006 /// @param handle_value the functor to invoke if @c self.has_value() is @c true .
1007 /// @return U, where @c U is the return type of @c F and is an instantiation of @c optional , which will be:
1008 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c true.
1009 /// * A @c U instantiated with @c nullopt otherwise.
1010 template <typename Self, typename F, constraints<std::enable_if_t<decays_to_v<Self, optional>>> = nullptr>
1011 static constexpr auto and_then(Self&& self, F&& handle_value)
1013 using f_optional_t = invoke_result_t<F, decltype(forward_like<Self>(std::forward<Self>(self).storage().value))>;
1014 if (self.has_value()) {
1015 return ::arene::base::invoke(
1016 std::forward<F>(handle_value),
1017 forward_like<Self>(std::forward<Self>(self).storage().value)
1018 );
1019 }
1020 return f_optional_t{};
1021 }
1022
1023 ///
1024 /// @brief deduced-this helper for @c optional::or_else<F>(F&&) .
1025 /// @tparam Self The qualified result type to invoke with. Must satisfy @c decays_to<Self,optional>
1026 /// @tparam F Type of the visiting functor. Must be invocable with no arguments and return @c optional<T> .
1027 /// @param self the instance of the result to use.
1028 /// @param handle_null the functor to invoke if @c self.has_value() is @c false .
1029 /// @return optional<T> which will be:
1030 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c false.
1031 /// * A instance of @c optional<T> constructed from forwarding @c self .
1032 template <typename Self, typename F, constraints<std::enable_if_t<decays_to_v<Self, optional>>> = nullptr>
1033 static constexpr auto or_else(Self&& self, F&& handle_null) -> optional<T> {
1034 if (self.has_value()) {
1035 return {std::forward<Self>(self)};
1036 }
1037 return ::arene::base::invoke(std::forward<F>(handle_null));
1038 }
1039
1040 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
1041 ///
1042 /// @brief deduced-this helper for @c optional::transform<F>(F&&) .
1043 /// @tparam Self The qualified result type to invoke with. Must satisfy @c decays_to<Self,optional>
1044 /// @tparam F Type of the visiting functor. Must be invocable with @c value_type matching the const/ref qualification
1045 /// of @c Self. May return any valid @c value_type
1046 /// @param self the instance of the result to use.
1047 /// @param value_transformer the functor to invoke if @c self.has_value() is @c true .
1048 /// @return optional<U>, where @c U is the return type of @c F , which will contain:
1049 /// * The result of invoking @c handle_value with the contents of @c value() if @c has_value() is @c true.
1050 /// * @c nullopt otherwise.
1051 template <typename Self, typename F, constraints<std::enable_if_t<decays_to_v<Self, optional>>> = nullptr>
1052 static constexpr auto transform(Self&& self, F&& value_transformer)
1054 using f_return_t = invoke_result_t<F, decltype(forward_like<Self>(std::forward<Self>(self).storage().value))>;
1055 using optional_t = optional<f_return_t>;
1056 if (self.has_value()) {
1057 return optional_t{::arene::base::invoke(
1058 std::forward<F>(value_transformer),
1059 forward_like<Self>(std::forward<Self>(self).storage().value)
1060 )};
1061 }
1062 return optional_t{};
1063 }
1064};
1065// parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: it is not hiding anything"
1066
1067} // namespace base
1068} // namespace arene
1069
1070// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
1071// parasoft-end-suppress AUTOSAR-A7_1_5-a-2
1072
1073#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_OPTIONAL_OPTIONAL_HPP_
A class that has the same implicit constructors as another type.
Definition implicit_constructor_base.hpp:31
backport of std::in_place_t from C++17
Definition in_place.hpp:23
Empty class type used to indicate that arene::base::optional does not contain a value.
Definition optional.hpp:68
constexpr nullopt_t(detail::unspecified_t) noexcept
constructor for nullopt_t
Definition optional.hpp:79
Implementation of std::optional for pre-C++17 compilers, and which replaces defined undefined behavio...
Definition optional.hpp:122
ARENE_IGNORE_ALL("-Wfloat-equal", "Can only suppress this here, not in user code; std::vector does the same thing")
constexpr auto operator*() const &noexcept -> T const &
Accesses the value in the optional.
Definition optional.hpp:320
constexpr auto operator*() const &&noexcept -> T const &&
Accesses the value in the optional.
Definition optional.hpp:330
constexpr void emplace(Args &&... args) noexcept(noexcept(std::declval< optional< T > >().construct_from(std::forward< Args >(args)...)))
Destroy the old value if there is one, and construct a new value.
Definition optional.hpp:440
constexpr optional(optional &&) noexcept(//NOLINTNEXTLINE(hicpp-noexcept-move) std::is_nothrow_move_constructible< T >::value)=default
Move constructor.
constexpr auto operator=(optional &&other) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value) -> optional &
Move assign.
Definition optional.hpp:275
static constexpr auto fast_inequality_check(optional const &lhs, optional< U > const &rhs) noexcept -> inequality_heuristic
Fast inequality check between two optional instances, where there is not a fast inequality check betw...
Definition optional.hpp:666
friend constexpr auto operator==(Self const &lhs, optional< U > const &rhs) noexcept -> bool
equality comparison between two optional instances
Definition optional.hpp:849
constexpr optional(optional< U > &&other) noexcept(noexcept(T(std::declval< U && >())))
Construct from another optional.
Definition optional.hpp:212
constexpr auto value_or(U &&default_value) &&noexcept(noexcept(optional::value_or(std::declval< optional && >(), std::forward< U >(default_value)))) -> T
Gets the value held in the optional, or else the provided default.
Definition optional.hpp:393
friend constexpr auto operator==(Self const &lhs, U const &rhs) noexcept -> bool
equality comparison between an optional instance and a value
Definition optional.hpp:815
constexpr optional(in_place_t, Args &&... args) noexcept(noexcept(T(std::forward< Args >(args)...)))
Construct from a set of arguments.
Definition optional.hpp:243
constexpr auto and_then(F &&handle_value) const &-> invoke_result_t< F, T const & >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:466
value_type value_type
Definition optional.hpp:148
constexpr optional(optional const &) noexcept(std::is_nothrow_copy_constructible< T >::value)=default
Copy constructor.
constexpr auto and_then(F &&handle_value) &-> invoke_result_t< F, T & >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:457
static constexpr auto three_way_compare(optional const &lhs, nullopt_t) noexcept -> strong_ordering
Three-way comparison between an optional and nullopt_t.
Definition optional.hpp:651
friend constexpr auto operator!=(optional const &lhs, nullopt_t) noexcept -> bool
inequality comparison between an optional instance and an explicit nullopt
Definition optional.hpp:872
constexpr auto transform(F &&value_transformer) const &-> optional< invoke_result_t< F, T const & > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:557
constexpr auto or_else(F &&handle_null) &&-> optional< T >
Monadic API which invokes a functor if has_value() is false.
Definition optional.hpp:524
constexpr auto transform(F &&value_transformer) const &&-> optional< invoke_result_t< F, T const && > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:575
friend constexpr auto operator!=(Self const &lhs, optional< U > const &rhs) noexcept -> bool
inequality comparison between two optional instances
Definition optional.hpp:896
constexpr auto operator=(optional const &other) noexcept(std::is_nothrow_copy_constructible< T >::value &&std::is_nothrow_copy_assignable< T >::value) -> optional &
Copy assign.
Definition optional.hpp:263
static constexpr auto three_way_compare(Self const &lhs, optional< U > const &rhs) noexcept -> strong_ordering
Three-way comparison between two optional instances.
Definition optional.hpp:714
~optional()=default
Default destructor.
constexpr auto value_or(U &&default_value) const &noexcept(noexcept(optional::value_or(std::declval< optional const & >(), std::forward< U >(default_value)))) -> T
Gets the value held in the optional, or else the provided default.
Definition optional.hpp:382
constexpr auto and_then(F &&handle_value) const &&-> invoke_result_t< F, T const && >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:484
auto operator=(U &&other) noexcept(std::is_nothrow_assignable< T, U >::value &&noexcept(std::declval< optional & >().construct_from(std::forward< U >(other)))) -> optional &
Assign from a value.
Definition optional.hpp:291
static constexpr auto three_way_compare(Self const &lhs, U const &rhs) noexcept -> strong_ordering
Three-way comparison between an optional instance and a value.
Definition optional.hpp:785
constexpr auto value_or_else(F &&default_generator) const &noexcept(noexcept(optional::value_or_else(std::declval< optional const & >(), std::forward< F >(default_generator)))) -> T
Gets the value held in the optional, or else invokes the provided callable to generate one.
Definition optional.hpp:416
ARENE_IGNORE_ALL("-Wsign-compare", "Can only suppress this here, not in user code; std::vector does the same thing")
friend constexpr auto operator==(optional const &lhs, nullopt_t) noexcept -> bool
equality comparison between an optional instance and an explicit nullopt
Definition optional.hpp:867
constexpr optional(nullopt_t) noexcept
Explicitly construct to an empty state.
Definition optional.hpp:168
constexpr auto transform(F &&value_transformer) &-> optional< invoke_result_t< F, T & > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:548
constexpr auto transform(F &&value_transformer) &&-> optional< invoke_result_t< F, T && > >
Calls a provided functor with value if it is populated, and produces a new optional with the return f...
Definition optional.hpp:566
constexpr auto value_or_else(F &&default_generator) &&noexcept(noexcept(optional::value_or_else(std::declval< optional && >(), std::forward< F >(default_generator)))) -> T
Gets the value held in the optional, or else invokes the provided callable to generate one.
Definition optional.hpp:427
friend class optional
Definition optional.hpp:965
constexpr auto and_then(F &&handle_value) &&-> invoke_result_t< F, T && >
Monadic API which invokes a functor with the contents of value() if has_value() is true.
Definition optional.hpp:475
friend constexpr void swap(optional &lhs, optional &rhs) noexcept(noexcept(lhs.swap(rhs)))
swaps the elements between two optionals.
Definition optional.hpp:636
static constexpr auto fast_inequality_check(optional const &lhs, U const &rhs) noexcept -> inequality_heuristic
Fast inequality check between an optional instance and a value, where there is a fast inequality chec...
Definition optional.hpp:760
friend constexpr auto operator!=(nullopt_t, optional const &rhs) noexcept -> bool
inequality comparison between an explicit nullopt and an optional instance
Definition optional.hpp:877
constexpr auto operator*() &&noexcept -> T &&
Accesses the value in the optional.
Definition optional.hpp:337
constexpr auto or_else(F &&handle_null) const &-> optional< T >
Monadic API which invokes a functor if has_value() is false.
Definition optional.hpp:512
constexpr auto operator=(nullopt_t) noexcept -> optional &
Reset via assignment.
Definition optional.hpp:253
constexpr optional(optional< U > const &other) noexcept(noexcept(T(std::declval< U const & >())))
Construct from another optional.
Definition optional.hpp:229
constexpr auto operator->() const noexcept -> T const *
Access a pointer to the value in the optional.
Definition optional.hpp:354
static constexpr auto fast_inequality_check(optional const &lhs, U const &) noexcept -> inequality_heuristic
Fast inequality check between an optional instance and a value, where there is not a fast inequality ...
Definition optional.hpp:741
constexpr void swap(optional &other) noexcept(//is_nothrow_swappable_v< U > &&std::is_nothrow_move_constructible< U >::value)
Exchanges the state of two optionals.
Definition optional.hpp:604
constexpr optional() noexcept
Default construct to an empty state.
Definition optional.hpp:154
constexpr auto operator*() &noexcept -> T &
Accesses the value in the optional.
Definition optional.hpp:325
constexpr auto operator->() noexcept -> T *
Access a pointer to the value in the optional.
Definition optional.hpp:358
static constexpr value_type value
Definition integral_constant.hpp:34
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
#define ARENE_CPP14_INLINE_VARIABLE_INIT(name,...)
Emulates C++17's inline variable declarations for an arbitrary initializer expression.
Definition cpp14_inline.hpp:95
Provides the decays_to_v<T, U> trait.
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
Implementation of a backport of std::forward_like.
Provides backports of std::in_place_t, std::in_place_type_t, and std::in_place_index_t from C++17.
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
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
constexpr bool decays_to_v
Determine if one type decays to be the same as another type.
three_way_compare_detail::compare_three_way compare_three_way
Function object that does a three-way comparison between operands, equivalent to std::compare_three_w...
Definition compare_three_way.hpp:214
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
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 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
nullopt_t null_opt_t
alias to maintain backwards compatibility with older versions of Arene Base.
Definition optional.hpp:85
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...
ARENE_NODISCARD auto on_scope_exit(CallableType &&func) noexcept(std::is_nothrow_constructible< remove_cvref_t< CallableType >, CallableType >::value) -> scope_guard< remove_cvref_t< CallableType > >
Declarative helper for constructing scope_guard instances.
Definition scope_guard.hpp:122
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
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
inequality_heuristic
An enumeration used for the return value of fast_inequality_check functions to indicate if two values...
Definition operators.hpp:696
@ may_be_equal_or_not_equal
The values checked may or may not be equal to each other; a more detailed check is required.
Definition operators.hpp:700
@ definitely_not_equal
The two values checked are definitely not equal to each other.
Definition operators.hpp:698
Definition accumulate.hpp:36
STL namespace.
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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,...
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