Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
variant.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_VARIANT_VARIANT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_VARIANT_HPP_
7
8// IWYU pragma: private, include "arene/base/variant.hpp"
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 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14
22#include "arene/base/detail/exceptions.hpp"
25#include "arene/base/stdlib_choice/add_pointer.hpp"
26#include "arene/base/stdlib_choice/addressof.hpp"
27#include "arene/base/stdlib_choice/cstddef.hpp"
28#include "arene/base/stdlib_choice/decay.hpp"
29#include "arene/base/stdlib_choice/declval.hpp"
30#include "arene/base/stdlib_choice/enable_if.hpp"
31#include "arene/base/stdlib_choice/equal_to.hpp"
32#include "arene/base/stdlib_choice/forward.hpp"
33#include "arene/base/stdlib_choice/ignore.hpp"
34#include "arene/base/stdlib_choice/initializer_list.hpp"
35#include "arene/base/stdlib_choice/integral_constant.hpp"
36#include "arene/base/stdlib_choice/is_constructible.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_default_constructible.hpp"
40#include "arene/base/stdlib_choice/is_move_assignable.hpp"
41#include "arene/base/stdlib_choice/is_move_constructible.hpp"
42#include "arene/base/stdlib_choice/is_reference.hpp"
43#include "arene/base/stdlib_choice/is_same.hpp"
44#include "arene/base/stdlib_choice/is_void.hpp"
45#include "arene/base/stdlib_choice/less.hpp"
46#include "arene/base/stdlib_choice/max_initializer_list_overload.hpp" // IWYU pragma: keep
48// IWYU pragma: no_include "arene/base/stdlib_choice/max_value_overload.hpp"
49#include "arene/base/stdlib_choice/move.hpp"
50#include "arene/base/stdlib_choice/remove_reference.hpp"
60
61#if ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED)
62#include "arene/base/variant/detail/bad_variant_access_exceptions_enabled.hpp" // IWYU pragma: export
63#else
64#include "arene/base/variant/detail/bad_variant_access_exceptions_disabled.hpp" // IWYU pragma: export
65#endif
66
67// parasoft-end-suppress AUTOSAR-A16_2_2-a
68
69// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
70
71// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
72// parasoft-begin-suppress CERT_CPP-ERR50-h "False positive: Exception specification is conditional"
73// parasoft-begin-suppress CERT_CPP-ERR55-a "False positive: Exception specification is conditional"
74
75// parasoft-begin-suppress CERT_CPP-ERR51-b "False positive: Thrown exception is propagated"
76// parasoft-begin-suppress AUTOSAR-A15_3_2-a "False positive: Thrown exception is propagated"
77// parasoft-begin-suppress AUTOSAR-A15_5_3-g "False positive: Thrown exception is propagated"
78// parasoft-begin-suppress CERT_CPP-ERR50-g "False positive: Thrown exception is propagated"
79// parasoft-begin-suppress AUTOSAR-M15_3_4-b "False positive: Thrown exception is propagated"
80// parasoft-begin-suppress AUTOSAR-A15_2_1-b "False positive: Throwing constructor not invoked before program startup"
81
82namespace arene {
83namespace base {
84template <typename... Ts>
85class variant;
86
87namespace variant_detail {
88
89/// @brief A utility class to recursively destroy/copy/move an object with the correct type base on the index of the
90/// type in the variant.
91/// @note This class should only be used when the type of the object to be constructed is unknown at compile time.
92/// Otherwise, @c construct_at should be used.
93/// @tparam Ts variadic template type argument of the types
94template <typename... Ts>
95class variant_util_impl;
96
97// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
98/// @brief copy construct @c T object from @c source to @c dest if @c selected is true
99/// @tparam T type of object
100/// @param selected enables the operation, otherwise function is no-op
101/// @param source pointer to the source object
102/// @param dest pointer to the destination object
103template <typename T>
104inline constexpr void copy_construct_object(bool selected, void const* source, void* dest) noexcept(
106) {
107 static_assert(std::is_copy_constructible<T>::value, "Type must be copyable");
108 if (selected) {
109 construct_at(static_cast<T*>(dest), *static_cast<T const*>(source));
110 }
111}
112// parasoft-end-suppress AUTOSAR-M3_3_2-a
113
114// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
115/// @brief copy assign @c T object from @c source to @c dest if @c selected is true
116/// @tparam T type of object
117/// @param selected enables the operation, otherwise function is no-op
118/// @param source pointer to the source object
119/// @param dest pointer to the destination object
120template <typename T>
121inline constexpr void copy_assign_object(bool selected, void const* source, void* dest) noexcept(
123) {
124 static_assert(std::is_copy_assignable<T>::value, "Type must be copy-assignable");
125 if (selected) {
126 *static_cast<T*>(dest) = *static_cast<T const*>(source);
127 }
128}
129// parasoft-end-suppress AUTOSAR-M3_3_2-a
130
131// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
132/// @brief move construct @c T object from @c source to @c dest if @c selected is true
133/// @tparam T type of object
134/// @param selected enables the operation, otherwise function is no-op
135/// @param source pointer to the source object
136/// @param dest pointer to the destination object
137template <typename T>
138inline constexpr void move_construct_object(bool selected, void* source, void* dest) noexcept(
140) {
141 static_assert(std::is_move_constructible<T>::value, "Type must be movable");
142 if (selected) {
143 construct_at(static_cast<T*>(dest), static_cast<std::remove_reference_t<T>&&>(*static_cast<T*>(source)));
144 }
145}
146// parasoft-end-suppress AUTOSAR-M3_3_2-a
147
148// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
149/// @brief move assign @c T object from @c source to @c dest if @c selected is true
150/// @tparam T type of object
151/// @param selected enables the operation, otherwise function is no-op
152/// @param source pointer to the source object
153/// @param dest pointer to the destination object
154template <typename T>
155inline constexpr void move_assign_object(bool selected, void* source, void* dest) noexcept(
157) {
158 static_assert(std::is_move_assignable<T>::value, "Type must be copy-assignable");
159 if (selected) {
160 *static_cast<T*>(dest) = static_cast<std::remove_reference_t<T>&&>(*static_cast<T*>(source));
161 }
162}
163// parasoft-end-suppress AUTOSAR-M3_3_2-a
164
165// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
166/// @brief destroy object @c T object at @c data if @c selected is true
167/// @tparam T type of object
168/// @param selected enables the operation, otherwise function is no-op
169/// @param data address of object to destroy
170template <typename T>
171inline constexpr void destroy_object(bool selected, void* data) noexcept {
172 if (selected) {
173 destroy_at(static_cast<T*>(data));
174 }
175}
176// parasoft-end-suppress AUTOSAR-M3_3_2-a
177
178// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
179/// @brief check objects pointed at @c lhs and @c rhs of type @c T for equality if @c selected is true
180/// @tparam T type of object
181/// @param selected enables the operation, otherwise function is no-op
182/// @param lhs pointer to the object on left
183/// @param rhs pointer to the object on right
184/// @return true if @c selected is true and objects @c rhs and @c lhs are equal
185template <typename T>
186inline constexpr auto check_object_equality(bool selected, void const* lhs, void const* rhs) noexcept -> bool {
187 if (!selected) {
188 return false;
189 }
190
191 return std::equal_to<T>{}(*static_cast<T const*>(lhs), *static_cast<T const*>(rhs));
192}
193// parasoft-end-suppress AUTOSAR-M3_3_2-a
194
195// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
196/// @brief check objects pointed at @c lhs and @c rhs of type @c T for less-than ordering if @c selected is true
197/// @tparam T type of object
198/// @param selected enables the operation, otherwise function is no-op
199/// @param lhs pointer to the object on left
200/// @param rhs pointer to the object on right
201/// @return true if @c selected is true and object @c lhs is less-than object @c rhs
202template <typename T>
203inline constexpr auto check_object_less_than(bool selected, void const* lhs, void const* rhs) noexcept -> bool {
204 if (!selected) {
205 return false;
206 }
207
208 return std::less<T>{}(*static_cast<T const*>(lhs), *static_cast<T const*>(rhs));
209}
210// parasoft-end-suppress AUTOSAR-M3_3_2-a
211
212/// @brief Recursive specialization of @c variant_util_impl
213/// @tparam T template type argument to be processed recursively with member functions
214/// @tparam Ts variadic template type argument of the types
215template <typename T, typename... Ts>
216class variant_util_impl<T, Ts...> {
217 public:
218 /// @brief Copy construct the object from @c source to @c dest if @c current_idx is equal to @c requested_idx.
219 /// Placement new is used to copy construct the object.
220 /// @param current_idx the index in @c Ts of the current type
221 /// @param requested_idx the index in @c Ts of the type to be copied
222 /// @param source pointer to the source object
223 /// @param dest pointer to the destination object
224 static constexpr void
225 copy(std::size_t current_idx, std::size_t requested_idx, void const* source, void* dest) noexcept(
228 ) {
229 copy_construct_object<T>(current_idx == requested_idx, source, dest);
230 variant_util_impl<Ts...>::copy(current_idx + 1U, requested_idx, source, dest);
231 }
232
233 /// @brief Copy assign the object from @c source to @c dest if @c current_idx is equal to @c requested_idx.
234 /// @param current_idx the index in @c Ts of the current type
235 /// @param requested_idx the index in @c Ts of the type to be copied
236 /// @param source pointer to the source object
237 /// @param dest pointer to the destination object
238 static constexpr void
239 copy_assign(std::size_t current_idx, std::size_t requested_idx, void const* source, void* dest) noexcept(
241 ) {
242 copy_assign_object<T>(current_idx == requested_idx, source, dest);
243 variant_util_impl<Ts...>::copy_assign(current_idx + 1U, requested_idx, source, dest);
244 }
245
246 /// @brief Move construct the object from @c source to @c dest if @c current_idx is equal to @c requested_idx.
247 /// Placement new is used to move construct the object.
248 /// @param current_idx the index in @c Ts of the current type
249 /// @param requested_idx the index in @c Ts of the type to be moved
250 /// @param source pointer to the source object
251 /// @param dest pointer to the destination object
252 static constexpr void move(std::size_t current_idx, std::size_t requested_idx, void* source, void* dest) noexcept(
255 ) {
256 move_construct_object<T>(current_idx == requested_idx, source, dest);
257 variant_util_impl<Ts...>::move(current_idx + 1U, requested_idx, source, dest);
258 }
259
260 /// @brief Move assign the object from @c source to @c dest if @c current_idx is equal to @c requested_idx.
261 /// @param current_idx the index in @c Ts of the current type
262 /// @param requested_idx the index in @c Ts of the type to be moved
263 /// @param source pointer to the source object
264 /// @param dest pointer to the destination object
265 static constexpr void
266 move_assign(std::size_t current_idx, std::size_t requested_idx, void* source, void* dest) noexcept(
268 ) {
269 move_assign_object<T>(current_idx == requested_idx, source, dest);
270 variant_util_impl<Ts...>::move_assign(current_idx + 1U, requested_idx, source, dest);
271 }
272
273 /// @brief Destroy the input object pointed at @c data if @c current_idx is equal to @c requested_idx.
274 /// @param current_idx the index in @c Ts of the current type
275 /// @param requested_idx the index in @c Ts of the type to be moved
276 /// @param data pointer to the object to destroy
277 static constexpr void destroy(std::size_t current_idx, std::size_t requested_idx, void* data) noexcept {
278 destroy_object<T>(current_idx == requested_idx, data);
279 variant_util_impl<Ts...>::destroy(current_idx + 1U, requested_idx, data);
280 }
281
282 /// @brief Check two objects pointed by @c rhs and @c lhs for equality if @c current_idx is equal to @c requested_idx.
283 /// @param current_idx the index in @c Ts of the current type
284 /// @param requested_idx the index in @c Ts of the type to be moved
285 /// @param lhs pointer to the lhs object
286 /// @param rhs pointer to the rhs object
287 /// @return the result of the comparison
288 static constexpr auto
289 check_equality(std::size_t current_idx, std::size_t requested_idx, void const* lhs, void const* rhs) noexcept
290 -> bool {
291 return check_object_equality<T>(current_idx == requested_idx, lhs, rhs) ||
292 variant_util_impl<Ts...>::check_equality(current_idx + 1U, requested_idx, lhs, rhs);
293 }
294
295 /// @brief Compare two objects pointed by @c rhs and @c lhs for ordering if @c current_idx is equal to @c
296 /// requested_idx.
297 /// @param current_idx the index in @c Ts of the current type
298 /// @param requested_idx the index in @c Ts of the type to be moved
299 /// @param lhs pointer to the lhs object
300 /// @param rhs pointer to the rhs object
301 /// @return the result of the comparison
302 static constexpr auto
303 check_less_than(std::size_t current_idx, std::size_t requested_idx, void const* lhs, void const* rhs) noexcept
304 -> bool {
305 return check_object_less_than<T>(current_idx == requested_idx, lhs, rhs) ||
306 variant_util_impl<Ts...>::check_less_than(current_idx + 1U, requested_idx, lhs, rhs);
307 }
308};
309
310/// @brief Specialization of the end of the recursion. No-op.
311template <>
312class variant_util_impl<> {
313 public:
314 // parasoft-begin-suppress AUTOSAR-M0_1_8-b "The intent of these function are that they do nothing"
315
316 /// @brief Copy construct object at @c to from object at @c from if @c current_idx equals @c idx
317 ///
318 /// This is a no-op for the recursive base specialization.
319 ///
320 static constexpr void
321 copy(std::size_t /*current_idx*/, std::size_t /*idx*/, void const* /*from*/, void* /*to*/) noexcept {}
322
323 /// @brief Copy assign object from @c from to @c to if @c current_idx equals @c idx
324 ///
325 /// This is a no-op for the recursive base specialization.
326 ///
327 static constexpr void
328 copy_assign(std::size_t /*current_idx*/, std::size_t /*idx*/, void const* /*from*/, void* /*to*/) noexcept {}
329 /// @brief Move construct object at @c to from object at @c from if @c current_idx equals @c idx
330 ///
331 /// This is a no-op for the recursive base specialization.
332 ///
333 static constexpr void move(std::size_t /*current_idx*/, std::size_t /*idx*/, void* /*from*/, void* /*to*/) noexcept {}
334
335 /// @brief Move assign object from @c from to @c to if @c current_idx equals @c idx
336 ///
337 /// This is a no-op for the recursive base specialization.
338 ///
339 static constexpr void
340 move_assign(std::size_t /*current_idx*/, std::size_t /*idx*/, void* /*from*/, void* /*to*/) noexcept {}
341
342 /// @brief Destroy object at @c data if @c current_idx equals @c idx
343 ///
344 /// This is a no-op for the recursive base specialization.
345 ///
346 static constexpr void destroy(std::size_t /*current_idx*/, std::size_t /*idx*/, void* /*data*/) noexcept {}
347
348 // parasoft-end-suppress AUTOSAR-M0_1_8-b
349
350 /// @brief Check two objects pointed by @c rhs and @c lhs for equality if @c current_idx is equal to @c requested_idx.
351 /// @param current_idx the index in @c Ts of the current type
352 /// @param requested_idx the index in @c Ts of the type to be moved
353 /// @param lhs pointer to the lhs object
354 /// @param rhs pointer to the rhs object
355 /// @return true if @c requested_idx is representing valueless_by_exception state
356 static constexpr auto check_equality(
357 std::size_t const current_idx, // NOLINT(bugprone-easily-swappable-parameters)
358 std::size_t const requested_idx, // NOLINT(bugprone-easily-swappable-parameters)
359 void const* const lhs,
360 void const* const rhs
361 ) noexcept -> bool {
362 std::ignore = current_idx;
363 std::ignore = lhs;
364 std::ignore = rhs;
365 // two variants in valueless_by_exception state are equal
366 return requested_idx == variant_npos;
367 }
368 /// @brief Compare two objects pointed by @c rhs and @c lhs for ordering if @c current_idx is equal to @c
369 /// @param current_idx the index in @c Ts of the current type
370 /// @param requested_idx the index in @c Ts of the type to be moved
371 /// @param lhs pointer to the lhs object
372 /// @param rhs pointer to the rhs object
373 /// @return always false
374 static constexpr auto check_less_than(
375 std::size_t const current_idx,
376 std::size_t const requested_idx,
377 void const* const lhs,
378 void const* const rhs
379 ) noexcept -> bool {
380 std::ignore = current_idx;
381 std::ignore = requested_idx;
382 std::ignore = lhs;
383 std::ignore = rhs;
384 // two variants in valueless_by_exception state are not ordered
385 return false;
386 }
387};
388
389/// @brief A utility class to recursively destroy/copy/move an object with the correct type base on the index of the
390/// type in the variant.
391/// @note This class should only be used when the type of the object to be constructed is unknown at compile time.
392/// Otherwise, @c construct_at should be used.
393/// @tparam Ts variadic template type argument of the types
394template <typename... Ts>
395class variant_util {
396 public:
397 /// @brief Copy construct the object from @c source to @c dest. Type of the object is @c idx -th from typelist @c Ts
398 /// Placement new is used to copy construct the object.
399 /// @param idx the index in @c Ts of the current type
400 /// @param source pointer to the source object
401 /// @param dest pointer to the destination object
402 inline static void copy(
403 std::size_t idx,
404 void const* source,
405 void* dest
406 ) noexcept(base::all_of_v<std::is_nothrow_copy_constructible<Ts>::value...>) {
407 variant_util_impl<Ts...>::copy(0U, idx, source, dest);
408 }
409
410 /// @brief Copy assign the object from @c source to @c dest. Type of the object is @c idx -th from typelist @c Ts
411 /// @param idx the index in @c Ts of the current type
412 /// @param source pointer to the source object
413 /// @param dest pointer to the destination object
414 inline static void copy_assign(
415 std::size_t idx,
416 void const* source,
417 void* dest
418 ) noexcept(base::all_of_v<std::is_nothrow_copy_assignable<Ts>::value...>) {
419 variant_util_impl<Ts...>::copy_assign(0U, idx, source, dest);
420 }
421
422 /// @brief Move construct the object from @c source to @c dest. Type of the object is @c idx -th from typelist @c Ts
423 /// Placement new is used to move construct the object.
424 /// @param idx the index in @c Ts of the current type
425 /// @param source pointer to the source object
426 /// @param dest pointer to the destination object
427 inline static void move(
428 std::size_t idx,
429 void* source,
430 void* dest
431 ) noexcept(base::all_of_v<std::is_nothrow_move_constructible<Ts>::value...>) {
432 variant_util_impl<Ts...>::move(0U, idx, source, dest);
433 }
434
435 /// @brief Move assign the object from @c source to @c dest. Type of the object is @c idx -th from typelist @c Ts
436 /// @param idx the index in @c Ts of the current type
437 /// @param source pointer to the source object
438 /// @param dest pointer to the destination object
439 inline static void move_assign(
440 std::size_t idx,
441 void* source,
442 void* dest
443 ) noexcept(base::all_of_v<std::is_nothrow_move_assignable<Ts>::value...>) {
444 variant_util_impl<Ts...>::move_assign(0U, idx, source, dest);
445 }
446
447 /// @brief Destroy the input object pointed at @c data. Type of the object is @c idx -th from typelist @c Ts
448 /// @param idx the index in @c Ts of the current type
449 /// @param data pointer to the object to destroy
450 inline static void destroy(std::size_t idx, void* data) noexcept { variant_util_impl<Ts...>::destroy(0U, idx, data); }
451
452 /// @brief Check two objects pointed by @c rhs and @c lhs for equality. Type of the objects is @c idx -th from
453 /// typelist @c Ts
454 /// @param idx the index in @c Ts of the current type
455 /// @param lhs pointer to the lhs object
456 /// @param rhs pointer to the rhs object
457 /// @return true if objects @c lhs and @c rhs are equal or @c idx represents valueless-by-exception state
458 inline static auto check_equality(std::size_t idx, void const* lhs, void const* rhs) noexcept -> bool {
459 return variant_util_impl<Ts...>::check_equality(0U, idx, lhs, rhs);
460 }
461
462 /// @brief Check two objects pointed by @c rhs and @c lhs for less-than ordering. Type of the objects is @c idx -th
463 /// from typelist @c Ts
464 /// @param idx the index in @c Ts of the current type
465 /// @param lhs pointer to the lhs object
466 /// @param rhs pointer to the rhs object
467 /// @return true if object @c lhs is less than @c rhs or false when @c idx represents valueless-by-exception state
468 inline static auto check_less_than(std::size_t idx, void const* lhs, void const* rhs) noexcept -> bool {
469 return variant_util_impl<Ts...>::check_less_than(0U, idx, lhs, rhs);
470 }
471};
472
473/// @brief Forward declaration. Helper to check if a given type can be constructed from the @c I 'th type in a variant.
474/// @tparam Variant the variant type
475/// @tparam I the index of the type to be checked
476template <class Variant, std::size_t I = 0>
477class convertible_checker;
478
479/// @brief Specialization of the empty(end of recursion).
480/// @tparam I always equal to the size of the real variant
481template <std::size_t I>
482class convertible_checker<variant<>, I> {
483 public:
484 /// @brief function used to determine if a type is convertible to a variant alternative
485 ///
486 /// This overload is defined in the recursive base so that all
487 /// convertible_checker specializations declare @c run
488 static void run() = delete;
489};
490
491/// @brief A recursive specialization of the convertible_checker.
492///
493/// This class has two static member functions @c run for each type in the variant. @c accepted_index will use the
494/// overload resolution rule to determine which type of the variant is the best match for the type given in the convert
495/// constructor of the variant. It is a compile error if two or more types in the variant can be converted from the
496/// given type with the same level of match.
497/// @tparam H the head type of the variant
498/// @tparam Ts the tail types of the variant
499/// @tparam I the index of the type to be checked
500template <class H, class... Ts, std::size_t I>
501class convertible_checker<variant<H, Ts...>, I> : convertible_checker<variant<Ts...>, I + 1> {
502 public:
503 using convertible_checker<variant<Ts...>, I + 1>::run;
504
505 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
506 /// @brief undefined function used to determine if a type is convertible to a variant alternative
507 /// @return integral constant containing the alternative index
508 static auto run(H const&, std::integral_constant<std::size_t, I>) -> std::integral_constant<std::size_t, I>;
509 // parasoft-end-suppress CERT_C-EXP37-a-3
510
511 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
512 /// @brief undefined function used to determine if a type is convertible to a variant alternative
513 /// @return integral constant containing the alternative index
514 static auto run(H&&, std::integral_constant<std::size_t, I>) -> std::integral_constant<std::size_t, I>;
515 // parasoft-end-suppress CERT_C-EXP37-a-3
516};
517
518/// @brief Helper to determine which type in the variant can be constructed from the given type.
519/// @tparam Variant the variant type
520/// @tparam T the type to be checked
521template <class Variant, class T, class = void>
522class accepted_index {};
523
524/// @brief Implementation of @c accepted_index .
525/// @tparam Variant the variant type
526/// @tparam T the type to be checked
527template <class Variant, class T>
528class accepted_index<Variant, T, void_t<decltype(convertible_checker<Variant>::run(std::declval<T>(), {}))>>
529 : public decltype(convertible_checker<Variant>::run(std::declval<T>(), {})) {};
530
531} // namespace variant_detail
532
533// parasoft-begin-suppress AUTOSAR-A12_0_1-a "False Positive: Copy constructor is defined or deleted as appropriate"
534// parasoft-begin-suppress AUTOSAR-A12_1_5-a "Delegating constructors would not reduce duplication"
535/// @brief a backport of C++17's @c std::variant
536/// @tparam Ts The set of alternatives that can be held by the variant.
537template <typename... Ts>
538// Justification: False positive about copy constructor not generated
539// NOLINTNEXTLINE(hicpp-special-member-functions)
541 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'util_type' does not hide another identifier"
542 /// @brief Alias to a utility class for this variant type.
543 using util_type = variant_detail::variant_util<Ts...>;
544 // parasoft-end-suppress AUTOSAR-A2_10_1-e
545
546 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'nth_type' does not hide another identifier"
547 /// @brief Obtains the alternative type for a given index
548 /// @tparam Index alternative type index
549 template <std::size_t Index>
550 using nth_type = type_lists::at_t<type_list<Ts...>, Index>;
551 // parasoft-end-suppress AUTOSAR-A2_10_1-e
552
553 static_assert(sizeof...(Ts) > 0, "Empty variant is not allowed");
554 static_assert(base::all_of_v<!std::is_reference<Ts>::value...>, "variant must have no reference alternative");
555
556 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'num_types' does not hide another identifier"
557 /// @brief Number of alternative types
558 static constexpr std::size_t num_types{sizeof...(Ts)};
559 // parasoft-end-suppress AUTOSAR-A2_10_1-e
560
561 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'max_size' does not hide another identifier"
562 /// @brief The maximum size of the alternative types
563 static constexpr std::size_t max_size{std::max({sizeof(Ts)...})};
564 // parasoft-end-suppress AUTOSAR-A2_10_1-e
565
566 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'max_align' does not hide another identifier"
567 /// @brief The maximum alignment of the alternative types
568 static constexpr std::size_t max_align{std::max({alignof(Ts)...})};
569 // parasoft-end-suppress AUTOSAR-A2_10_1-e
570
571 /// @brief The type of the argument to the "copy assignment operator".
572 ///
573 /// Either @c variant to provide a copy assignment when all the alternative types are copy constructible and copy
574 /// assignable, or @c non_constructible_dummy otherwise.
575 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'copy_assignment_arg' does not hide another identifier"
576 using copy_assignment_arg = conditional_t<
578 variant,
580 // parasoft-end-suppress AUTOSAR-A2_10_1-e
581
582 /// @brief the type of the argument to the "copy assignment operator". The opposite to @c copy_assignment_arg .
583 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'dummy_copy_assign_source' does not hide another
584 // identifier"
585 using dummy_copy_assign_source = conditional_t<
588 variant>;
589 // parasoft-end-suppress AUTOSAR-A2_10_1-e
590
591 /// @brief The type of the argument to the "copy constructor".
592 ///
593 /// Either @c variant to provide a copy ctor when all the alternative types are copy constructible, or
594 /// @c non_constructible_dummy otherwise.
595 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'copy_ctor_arg' does not hide another identifier"
596 using copy_ctor_arg =
598 // parasoft-end-suppress AUTOSAR-A2_10_1-e
599
600 /// @brief The type of the argument to the "copy constructor". The opposite to @c copy_ctor_arg.
601 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'dummy_copy_ctor_source' does not hide another
602 // identifier"
603 using dummy_copy_ctor_source =
605 // parasoft-end-suppress AUTOSAR-A2_10_1-e
606
607 /// @brief The type of the argument to the "move assignment operator".
608 ///
609 /// Either @c variant to provide a move assignment when all the alternative types are move constructible and move
610 /// assignable, or @c non_constructible_dummy otherwise.
611 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'move_assignment_arg' does not hide another identifier"
612 using move_assignment_arg = conditional_t<
614 variant,
616 // parasoft-end-suppress AUTOSAR-A2_10_1-e
617
618 /// @brief The type of the argument to the "move assignment operator". The opposite to @c move_assignment_arg.
619 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'dummy_move_assign_source' does not hide another
620 // identifier"
621 using dummy_move_assign_source = conditional_t<
624 variant>;
625 // parasoft-end-suppress AUTOSAR-A2_10_1-e
626
627 /// @brief The type of the argument to the "move constructor".
628 ///
629 /// Either @c variant to provide a move ctor when all the alternative types are move constructible, or
630 /// @c non_constructible_dummy otherwise.
631 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'move_ctor_arg' does not hide another identifier"
632 using move_ctor_arg =
634 // parasoft-end-suppress AUTOSAR-A2_10_1-e
635
636 /// @brief The type of the argument to the "move constructor". The opposite to @c move_ctor_arg.
637 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'dummy_move_ctor_source' does not hide another
638 // identifier"
639 using dummy_move_ctor_source =
641 // parasoft-end-suppress AUTOSAR-A2_10_1-e
642
643 public:
644 /// @brief Default constructor, which initializes the variant to a default constructed instance of its 0'th
645 /// alternative.
646 ///
647 /// @pre Only participates in overload resolution if the first type in @c Ts is default constructible.
648 template <
649 typename T0 = nth_type<0>,
651 variant() noexcept(std::is_nothrow_default_constructible<T0>::value)
653 idx_{} {
654 // Justification: It's intended to use reinterpret_cast to construct
655 // the object on an uninitialized address.
656 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
657 construct_at(reinterpret_cast<T0*>(data_.data()));
658 }
659
660 /// @brief Copy Constructor from another variant of the same alternative types.
661 ///
662 /// If the other variant is valueless, this variant will also be valueless.
663 /// @pre Only participates in overload resolution if every type in @c Ts is copy constructible.
664 /// @param other the other variant to copy from
665 // parasoft-begin-suppress AUTOSAR-A13_3_1-a "False positive: No forwarding reference"
666 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
669 idx_{other.idx_} {
670 // util_type::copy is no-op for valueless-by-exception
671 util_type::copy(other.idx_, other.data_.data(), data_.data());
672 }
673 // parasoft-end-suppress AUTOSAR-A13_3_1-a
674
675 /// @brief Copy Constructor from another variant of the same alternative types.
676 ///
677 /// If the other variant is valueless, this variant will also be valueless.
678 /// @pre Only participates in overload resolution if every type in @c Ts is copy constructible.
679 /// @param other the other variant to copy from
680 variant(dummy_copy_ctor_source const& other) = delete;
681
682 // parasoft-begin-suppress AUTOSAR-A15_5_1-b-2 "False positive: Conditionally noxecept"
683 // parasoft-begin-suppress AUTOSAR-A8_4_6-a-2 "False positive: The data is moved via the 'util_type::move' function"
684 // parasoft-begin-suppress AUTOSAR-A8_4_5-a-2 "False positive: The data is moved via the 'util_type::move' function"
685 // parasoft-begin-suppress AUTOSAR-A12_8_4-a-2 "False positive: The data is moved via the 'util_type::move' function"
686 /// @brief Move Constructor from another variant.
687 ///
688 /// If the other variant is valueless, this variant will also be valueless.
689 /// @pre Only participates in overload resolution if every type in @c Ts is move constructible.
690 /// @param other the other variant to move from
691 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
694 idx_{other.idx_} {
695 // util_type::move is no-op for valueless-by-exception
696 util_type::move(other.idx_, other.data_.data(), data_.data());
697 }
698 // parasoft-env-suppress AUTOSAR-A12_8_4-a-2
699 // parasoft-env-suppress AUTOSAR-A8_4_5-a-2
700 // parasoft-env-suppress AUTOSAR-A8_4_6-a-2
701 // parasoft-env-suppress AUTOSAR-A15_5_1-b-2
702
703 /// @brief Move Constructor form another variant.
704 ///
705 /// If the other variant is valueless, this variant will also be valueless.
706 /// @pre Only participates in overload resolution if every type in @c Ts is move constructible.
707 /// @param other the other variant to move from
708 variant(dummy_move_ctor_source&& other) = delete;
709
710 // parasoft-begin-suppress AUTOSAR-A2_7_2-a-2 "False positive: no commented-out code"
711 /// @brief Constructor that initializes this variant from one of its alternative types.
712 ///
713 /// This constructor will pick the constructor overload of the alternative type @c Tj that <c> T_i x[] = {
714 /// std::forward<T>(value) }; </c> is valid. If more than one alternative type satisfies this condition, the
715 /// compilation will fail.
716 /// @tparam T the type to construct this variant
717 /// @tparam I the deduced index of the alternative type that T can construct
718 /// @tparam Tj the deduced alternative type that T can construct
719 /// @param value the object to forward into the variant
720 // parasoft-end-suppress AUTOSAR-A2_7_2-a-2
721 template <
722 typename T,
723 std::size_t I = variant_detail::accepted_index<variant, T>::type::value,
724 typename Tj = nth_type<I>,
728 // Justification: The forwarding reference constructor won't hide copy/move
729 // constructor since the constraints ensures T cannot be another variant.
730 // Justification: std::variant doesn't require this to be explicit.
731 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload,google-explicit-constructor,hicpp-explicit-conversions)
734 idx_(I) {
735 // Justification: It's intended to use reinterpret_cast to construct
736 // the object on an uninitialized address.
737 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
738 construct_at(reinterpret_cast<Tj*>(data_.data()), std::forward<T>(value));
739 }
740
741 /// @brief Constructor that initializes this variant from a specified alternative index.
742 ///
743 /// @tparam I the alternative index to construct. Must be less than @c num_types.
744 /// @tparam Args the types of the arguments to forward to the constructor of the selected alternative.
745 /// The selected alternative must be constructible from these types.
746 /// @param args the arguments to forward to the constructor of the selected alternative
747 template <
748 std::size_t I,
749 typename... Args,
751 std::enable_if_t<(I < num_types)>,
752 std::enable_if_t<std::is_constructible<nth_type<I>, Args...>::value>> = nullptr>
753 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
754 explicit variant(in_place_index_t<I>, Args&&... args) noexcept(
756 )
757 // parasoft-end-suppress CERT_C-EXP37-a-3
759 idx_(I) {
760 // Justification: It's intended to use reinterpret_cast to construct
761 // the object on an uninitialized address.
762 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
763 construct_at(reinterpret_cast<nth_type<I>*>(data_.data()), std::forward<Args>(args)...);
764 }
765
766 /// @brief Constructor that initializes this variant from a specified alternative index via an initializer list
767 ///
768 /// @tparam I the alternative index to construct. Must be less than @c num_types.
769 /// @tparam U the type of the elements in the initializer list
770 /// @tparam Args the types of the arguments to forward to the constructor of the selected alternative.
771 /// The selected alternative must be constructible from an lvalue initializer list of @c U and these types.
772 /// @param init_list the initializer list to forward to the constructor of the selected alternative
773 /// @param args the arguments to forward to the constructor of the selected alternative
774 template <
775 std::size_t I,
776 typename U,
777 typename... Args,
779 std::enable_if_t<(I < num_types)>,
781 variant(in_place_index_t<I>, std::initializer_list<U> init_list, Args&&... args) noexcept(
783 )
785 idx_(I) {
786 // Justification: It's intended to use reinterpret_cast to construct
787 // the object on an uninitialized address.
788 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
789 construct_at(reinterpret_cast<nth_type<I>*>(data_.data()), init_list, std::forward<Args>(args)...);
790 }
791
792 /// @brief Copy Assignment operator from another variant of the same alternative types.
793 ///
794 /// If the other type is valueless, or exception is thrown during copying, this variant will also be valueless.
795 /// @pre Only participates in overload resolution if every type in @c Ts is copy-assignable.
796 /// @param rhs the other variant to copy from
797 auto operator=(copy_assignment_arg const& rhs
799 -> variant& {
800 if (this != &rhs) {
801 if (index() != rhs.index()) { // different
802 destroy();
803 util_type::copy(rhs.idx_, rhs.data_.data(), data_.data()); // no-op for valueless-by-exception
804 idx_ = rhs.idx_;
805 } else {
806 util_type::copy_assign(rhs.idx_, rhs.data_.data(), data_.data()); // no-op for valueless-by-exception
807 }
808 }
809
810 return *this;
811 }
812 /// @brief Copy Assignment operator from another variant of the same alternative types.
813 ///
814 /// If the other type is valueless, or exception is thrown during copying, this variant will also be valueless.
815 /// @pre Only participates in overload resolution if every type in @c Ts is copy-assignable.
816 /// @param rhs the other variant to copy from
817 auto operator=(dummy_copy_assign_source const& rhs) -> variant& = delete;
818
819 // parasoft-begin-suppress AUTOSAR-A15_5_1-b-2 "False positive: Conditionally noxecept"
820 // parasoft-begin-suppress AUTOSAR-A8_4_6-a-2 "False positive: The data is moved via the 'util_type::move*' function"
821 // parasoft-begin-suppress AUTOSAR-A8_4_5-a-2 "False positive: The data is moved via the 'util_type::move*' function"
822 // parasoft-begin-suppress AUTOSAR-A12_8_4-a-2 "False positive: The data is moved via the 'util_type::move*' function"
823 /// @brief Move Assignment operator from another variant of the same alternative types.
824 ///
825 /// If the other type is valueless, this variant will also be valueless.
826 /// @pre Only participates in overload resolution if every type in @c Ts is move-assignable.
827 /// @param rhs the other variant to move from
828 auto operator=(move_assignment_arg&& rhs
830 -> variant& {
831 if (this != &rhs) {
832 if (index() != rhs.index()) {
833 destroy();
834 util_type::move(rhs.idx_, rhs.data_.data(), data_.data()); // no-op for valueless-by-exception
835 idx_ = rhs.idx_;
836 } else {
837 util_type::move_assign(rhs.idx_, rhs.data_.data(), data_.data()); // no-op for valueless-by-exception
838 }
839 }
840
841 return *this;
842 }
843
844 // parasoft-end-suppress AUTOSAR-A12_8_4-a-2
845 // parasoft-end-suppress AUTOSAR-A8_4_5-a-2
846 // parasoft-end-suppress AUTOSAR-A8_4_6-a-2
847 // parasoft-end-suppress AUTOSAR-A15_5_1-b-2
848
849 /// @brief Move Assignment operator from another variant of the same alternative types.
850 ///
851 /// If the other type is valueless, this variant will also be valueless.
852 /// @pre Only participates in overload resolution if every type in @c Ts is move-assignable.
853 /// @param rhs the other variant to move from
854 auto operator=(dummy_move_assign_source&& rhs) -> variant& = delete;
855
856 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
857 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: Function is namespace scope and used in other
858 // translation units"
859 /// @brief Equality comparison
860 /// @pre The behavior is undefined if not all the alternatives support equality comparison.
861 /// @param lhs variant to compare
862 /// @param rhs the other variant to compare
863 /// @return true if both variants are valueless, or both variants hold the same alternative type and the value of that
864 /// alternative type is equal.
865 /// @return false otherwise.
866 friend auto operator==(variant const& lhs, variant const& rhs) noexcept -> bool {
867 if (lhs.index() != rhs.index()) {
868 return false;
869 }
870
871 // util_type::check_equality returns true for valueless-by-exception state
872 return util_type::check_equality(
873 lhs.idx_,
874 static_cast<void const*>(lhs.data_.data()),
875 static_cast<void const*>(rhs.data_.data())
876 );
877 }
878 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
879 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
880
881 // parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "False positive: comparison operators are permitted"
882 // parasoft-begin-suppress AUTOSAR-A0_1_3-a-2 "False positive: Function is namespace scope and used in other
883 // translation units"
884 /// @brief Less-than comparison
885 /// @pre The behavior is undefined if not all the alternatives support less than comparison.
886 /// @param lhs variant to compare
887 /// @param rhs the other variant to compare
888 /// @return false if either variant is valueless
889 /// @return Otherwise, returns the result of invoking @c std::less on the held alternatives of the two variants.
890 friend auto operator<(variant const& lhs, variant const& rhs) noexcept -> bool {
891 if (lhs.index() != rhs.index()) {
892 return (lhs.index() + 1U) < (rhs.index() + 1U);
893 }
894
895 // util_type::check_less_than returns false for valueless-by-exception state
896 return util_type::check_less_than(
897 lhs.idx_,
898 static_cast<void const*>(lhs.data_.data()),
899 static_cast<void const*>(rhs.data_.data())
900 );
901 }
902 // parasoft-end-suppress AUTOSAR-A0_1_3-a-2
903 // parasoft-end-suppress AUTOSAR-A11_3_1-a-2
904
905 /// @brief Get the index of the alternative type that this variant currently holds.
906 /// @return std::size_t the index of the alternative type that this variant currently holds, or @c variant_npos if the
907 /// variant is valueless.
908 auto index() const noexcept -> std::size_t { return idx_; }
909
910 /// @brief Emplaces a value into the variant by index.
911 /// @tparam I the index of the alternative type to emplace
912 /// @tparam Args the variadic argument type to pass into the constructor of
913 /// the alternative type
914 /// @param args Arguments to be forwarded to the constructor of the emplaced type.
915 /// @return A reference to the emplaced object.
916 /// @post If the variant was not valueless, the currently held value is destroyed
917 /// @post The variant will hold the value the returned reference points to.
918 /// @post @c holds_alternative<I>(*this) will return true
919 /// @post index() will return @c I
920 template <std::size_t I, typename... Args>
921 auto emplace(Args&&... args) noexcept(std::is_nothrow_constructible<nth_type<I>, Args...>::value) -> nth_type<I>& {
922 destroy();
923 using selected_type = nth_type<I>;
924 // Justification: It's intended to use reinterpret_cast to construct
925 // the object on an uninitialized address.
926 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
927 construct_at(reinterpret_cast<selected_type*>(data_.data()), std::forward<Args>(args)...);
928 idx_ = I;
929 return get<I>();
930 }
931
932 /// @brief Emplaces a value into the variant by index.
933 /// @tparam I the index of the alternative type to emplace
934 /// @tparam Args the variadic argument type to pass into the constructor of
935 /// the alternative type
936 /// @param init An initializer list to forward to the constructor of the emplaced type.
937 /// @param args Arguments to be forwarded to the constructor of the emplaced type.
938 /// @return A reference to the emplaced object.
939 /// @post If the variant was not valueless, the currently held value is destroyed
940 /// @post The variant will hold the value the returned reference points to.
941 /// @post @c holds_alternative<I>(*this) will return true
942 /// @post index() will return @c I
943 template <std::size_t I, typename U, typename... Args>
944 auto emplace(std::initializer_list<U> init, Args&&... args) noexcept(
946 ) -> nth_type<I>& {
947 destroy();
948 using selected_type = nth_type<I>;
949 // Justification: It's intended to use reinterpret_cast to construct
950 // the object on an uninitialized address.
951 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
952 construct_at(reinterpret_cast<selected_type*>(data_.data()), init, std::forward<Args>(args)...);
953 idx_ = I;
954 return get<I>();
955 }
956
957 /// @brief Emplaces a value into the variant by type.
958 /// @tparam T the alternative type to emplace
959 /// @param args Arguments to be forwarded to the constructor of the emplaced type.
960 /// @return A reference to the emplaced object.
961 /// @post If the variant was not valueless, the currently held value is destroyed
962 /// @post The variant will hold the value the returned reference points to.
963 /// @post @c holds_alternative<T>(*this) will return true
964 /// @post index() will return the the equivalent of <c> index_of_v<T, Ts...> </c>
965 template <typename T, typename... Args>
966 auto emplace(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args...>::value) -> T& {
967 destroy();
968 // Justification: It's intended to use reinterpret_cast to construct
969 // the object on an uninitialized address.
970 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
971 construct_at(reinterpret_cast<T*>(data_.data()), std::forward<Args>(args)...);
972 idx_ = base::index_of_v<T, Ts...>;
973 return get<T>();
974 }
975
976 /// @brief Emplaces a value into the variant by type.
977 /// @tparam T the alternative type to emplace
978 /// @param init An initializer list to forward to the constructor of the emplaced type.
979 /// @param args Arguments to be forwarded to the constructor of the emplaced type.
980 /// @return A reference to the emplaced object.
981 /// @post If the variant was not valueless, the currently held value is destroyed
982 /// @post The variant will hold the value the returned reference points to.
983 /// @post @c holds_alternative<T>(*this) will return true
984 /// @post index() will return the the equivalent of <c> index_of_v<T, Ts...> </c>
985 template <typename T, typename U, typename... Args>
986 auto emplace(std::initializer_list<U> init, Args&&... args) noexcept(
988 ) -> T& {
989 destroy();
990 // Justification: It's intended to use reinterpret_cast to construct
991 // the object on an uninitialized address.
992 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
993 construct_at(reinterpret_cast<T*>(data_.data()), init, std::forward<Args>(args)...);
994 idx_ = base::index_of_v<T, Ts...>;
995 return get<T>();
996 }
997
998 /// @brief Get by type.
999 /// @tparam T the alternative type to get
1000 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1001 /// by the variant does not match the type requested.
1002 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1003 /// match the type requested else @c ARENE_PRECONDITION violation.
1004 /// @return T& The reference to the held object.
1005 template <typename T>
1006 auto get() & noexcept(!detail::are_exceptions_enabled_v) -> T& {
1007 return get<base::index_of_v<T, Ts...>>();
1008 }
1009 /// @brief Get by type.
1010 /// @tparam T the alternative type to get
1011 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1012 /// by the variant does not match the type requested.
1013 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1014 /// match the type requested else @c ARENE_PRECONDITION violation.
1015 /// @return T const& The reference to the held object.
1016 template <typename T>
1017 auto get() const& noexcept(!detail::are_exceptions_enabled_v) -> T const& {
1018 return get<base::index_of_v<T, Ts...>>();
1019 }
1020
1021 /// @brief Get by type.
1022 /// @tparam T the alternative type to get
1023 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1024 /// by the variant does not match the type requested.
1025 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1026 /// match the type requested else @c ARENE_PRECONDITION violation.
1027 /// @return T&& The reference to the held object.
1028 template <typename T>
1029 auto get() && noexcept(!detail::are_exceptions_enabled_v) -> T&& {
1031 }
1032 /// @brief Get by type.
1033 /// @tparam T the alternative type to get
1034 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1035 /// by the variant does not match the type requested.
1036 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1037 /// match the type requested else @c ARENE_PRECONDITION violation.
1038 /// @return T const&& The reference to the held object.
1039 template <typename T>
1040 auto get() const&& noexcept(!detail::are_exceptions_enabled_v) -> T const&& {
1041 // parasoft-begin-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1043 // parasoft-end-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1044 }
1045
1046 /// @brief Get the object stored in this variant by the index
1047 /// @tparam Index the index into @c Ts of the alternative to get
1048 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object
1049 /// currently held by the variant does not match the index requested.
1050 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1051 /// match the type requested else @c ARENE_PRECONDITION violation.
1052 /// @return nth_type<Index>& The reference to the held object.
1053 template <std::size_t Index>
1054 auto get() & noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index>& {
1055 return get_impl<Index, nth_type<Index>&>(*this);
1056 }
1057
1058 /// @brief Get the object stored in this variant by the index
1059 /// @tparam Index the index into @c Ts of the alternative to get
1060 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object
1061 /// currently held by the variant does not match the index requested.
1062 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1063 /// match the type requested else @c ARENE_PRECONDITION violation.
1064 /// @return nth_type<Index> const& The reference to the held object.
1065 template <std::size_t Index>
1066 auto get() const& noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index> const& {
1067 return get_impl<Index, nth_type<Index> const&>(*this);
1068 }
1069
1070 /// @brief Get the object stored in this variant by the index
1071 /// @tparam Index the index into @c Ts of the alternative to get
1072 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object
1073 /// currently held by the variant does not match the index requested.
1074 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1075 /// match the type requested else @c ARENE_PRECONDITION violation.
1076 /// @return nth_type<Index>&& The reference to the held object.
1077 template <std::size_t Index>
1078 auto get() && noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index>&& {
1079 return get_impl<Index, nth_type<Index>&&>(std::move(*this));
1080 }
1081
1082 /// @brief Get the object stored in this variant by the index
1083 /// @tparam Index the index into @c Ts of the alternative to get
1084 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object
1085 /// currently held by the variant does not match the index requested.
1086 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1087 /// match the type requested else @c ARENE_PRECONDITION violation.
1088 /// @return nth_type<Index> const&& The reference to the held object.
1089 template <std::size_t Index>
1090 auto get() const&& noexcept(!detail::are_exceptions_enabled_v) -> nth_type<Index> const&& {
1091 // parasoft-begin-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1092 return get_impl<Index, nth_type<Index> const&&>(std::move(*this));
1093 // parasoft-end-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1094 }
1095
1096 /// @brief Check if the variant is valueless.
1097 ///
1098 /// A variant becomes valueless if and only if an exception is thrown during copy or move initialization
1099 /// @return true if the variant does not hold a value
1100 /// @return false if the variant holds a value.
1101 constexpr auto valueless_by_exception() const noexcept -> bool { return idx_ == variant_npos; }
1102
1103 /// @brief Calls the destructor of the alternative type.
1104 ~variant() { destroy(); }
1105
1106 private:
1107 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'get_impl' does not hide another identifier"
1108 /// @brief Get the object stored in this variant by the index
1109 /// @tparam Index the index into @c Ts of the alternative to get
1110 /// @tparam Alternative (const-qualified) alternative type for Index
1111 /// @tparam Self (const-qualified) variant type
1112 /// @param self reference to (const-qualified) variant type
1113 /// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object
1114 /// currently held by the variant does not match the index requested.
1115 /// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1116 /// match the type index requested else @c ARENE_PRECONDITION violation.
1117 /// @return The reference to the held object.
1118 template <std::size_t Index, typename AlternativeRefT, typename Self>
1119 static auto get_impl(Self&& self) noexcept(!detail::are_exceptions_enabled_v) -> AlternativeRefT {
1120 variant_detail::variant_must_have_value(Index, self.idx_);
1123 "-Wstrict-aliasing",
1124 "It's safe to use reinterpret_cast here since the check above ensures that the type of the object "
1125 "currently held by the variant is the same as the type requested."
1126 );
1127 return ::arene::base::forward_like<Self>(
1128 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1129 *reinterpret_cast<std::remove_reference_t<AlternativeRefT>*>(std::forward<Self>(self).data_.data())
1130 );
1132 }
1133 // parasoft-end-suppress AUTOSAR-A2_10_1-e
1134
1135 /// @brief Destroy the object held by this variant if it's not valueless.
1136 void destroy() noexcept {
1137 // util_type::destroy is no-op for valueless-by-exception
1138 util_type::destroy(idx_, data_.data());
1139 idx_ = variant_npos;
1140 }
1141
1142 /// @brief index of the active alternative
1144 /// @brief actual type and data storage
1145 alignas(max_align) array<byte, max_size> data_{};
1146};
1147// parasoft-end-suppress AUTOSAR-A12_1_5-a
1148// parasoft-end-suppress AUTOSAR-A12_0_1-a
1149
1150/// @brief Get value held by a variant by type.
1151/// @tparam T the alternative type to get
1152/// @param var variant to get from
1153/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1154/// by the variant does not match the type requested.
1155/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1156/// match the type requested else @c ARENE_PRECONDITION violation.
1157/// @return The reference to the held object.
1158template <typename T, typename... Ts>
1159inline constexpr auto get(variant<Ts...>& var) noexcept(!detail::are_exceptions_enabled_v) -> T& {
1160 static_assert(!std::is_void<T>::value, "T should not be void");
1161 return var.template get<T>();
1162}
1163
1164/// @brief Get value held by a variant by type.
1165/// @tparam T the alternative type to get
1166/// @param var variant to get from
1167/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1168/// by the variant does not match the type requested.
1169/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1170/// match the type requested else @c ARENE_PRECONDITION violation.
1171/// @return The reference to the held object.
1172template <typename T, typename... Ts>
1173inline constexpr auto get(variant<Ts...> const& var) noexcept(!detail::are_exceptions_enabled_v) -> T const& {
1174 static_assert(!std::is_void<T>::value, "T should not be void");
1175 return var.template get<T>();
1176}
1177
1178/// @brief Get value held by a variant by type.
1179/// @tparam T the alternative type to get
1180/// @param var variant to get from
1181/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1182/// by the variant does not match the type requested.
1183/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1184/// match the type requested else @c ARENE_PRECONDITION violation.
1185/// @return The reference to the held object.
1186template <typename T, typename... Ts>
1187inline constexpr auto get(variant<Ts...>&& var) noexcept(!detail::are_exceptions_enabled_v) -> T&& {
1188 static_assert(!std::is_void<T>::value, "T should not be void");
1189 return std::move(var).template get<T>();
1190}
1191
1192/// @brief Get value held by a variant by type.
1193/// @tparam T the alternative type to get
1194/// @param var variant to get from
1195/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held
1196/// by the variant does not match the type requested.
1197/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must
1198/// match the type requested else @c ARENE_PRECONDITION violation.
1199/// @return The reference to the held object.
1200template <typename T, typename... Ts>
1201inline constexpr auto get(variant<Ts...> const&& var) noexcept(!detail::are_exceptions_enabled_v) -> T const&& {
1202 static_assert(!std::is_void<T>::value, "T should not be void");
1203 // parasoft-begin-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1204 return std::move(var).template get<T>();
1205 // parasoft-end-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1206}
1207
1208/// @brief Get value held by a variant by index.
1209/// @tparam Index the index into @c Ts of the alternative to get
1210/// The variant to get from is passed as @c var.
1211/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1212/// @param var variant to get from
1213/// @endcond
1214/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently
1215/// held by the variant does not match the index requested.
1216/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1217/// match the type requested else @c ARENE_PRECONDITION violation.
1218/// @return The reference to the held object.
1219// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1220template <std::size_t Index, typename... Ts>
1221constexpr auto get(variant<Ts...>& var) noexcept(!detail::are_exceptions_enabled_v)
1222 -> variant_alternative_t<Index, variant<Ts...>>& {
1223 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1224 return var.template get<Index>();
1225}
1226// parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1227/// @brief Get value held by a variant by index.
1228/// @tparam Index the index into @c Ts of the alternative to get
1229/// The variant to get from is passed as @c var.
1230/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1231/// @param var variant to get from
1232/// @endcond
1233/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently
1234/// held by the variant does not match the index requested.
1235/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1236/// match the type requested else @c ARENE_PRECONDITION violation.
1237/// @return The reference to the held object.
1238// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1239template <std::size_t Index, typename... Ts>
1240constexpr auto get(variant<Ts...> const& var) noexcept(!detail::are_exceptions_enabled_v)
1241 -> variant_alternative_t<Index, variant<Ts...>> const& {
1242 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1243 return var.template get<Index>();
1244}
1245// parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1246
1247/// @brief Get value held by a variant by index.
1248/// @tparam Index the index into @c Ts of the alternative to get
1249/// The variant to get from is passed as @c var.
1250/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1251/// @param var variant to get from
1252/// @endcond
1253/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently
1254/// held by the variant does not match the index requested.
1255/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the index type of the object currently held by the variant must
1256/// match the type requested else @c ARENE_PRECONDITION violation.
1257/// @return The reference to the held object.
1258// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1259template <std::size_t Index, typename... Ts>
1260constexpr auto get(variant<Ts...>&& var) noexcept(!detail::are_exceptions_enabled_v)
1262 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1263 return std::move(var).template get<Index>();
1264}
1265// parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1266/// @brief Get value held by a variant by index.
1267/// @tparam Index the index into @c Ts of the alternative to get
1268/// The variant to get from is passed as @c var.
1269/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1270/// @param var variant to get from
1271/// @endcond
1272/// @throw bad_variant_access if @c ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently
1273/// held by the variant does not match the index requested.
1274/// @pre If @c ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must
1275/// match the type requested else @c ARENE_PRECONDITION violation.
1276/// @return The reference to the held object.
1277// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1278template <std::size_t Index, typename... Ts>
1279constexpr auto get(variant<Ts...> const&& var) noexcept(!detail::are_exceptions_enabled_v)
1280 -> variant_alternative_t<Index, variant<Ts...>> const&& {
1281 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1282 // parasoft-begin-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1283 return std::move(var).template get<Index>();
1284 // parasoft-end-suppress AUTOSAR-A18_9_3-a "Must maintain the reference type of the return value"
1285}
1286// parasoft-end-suppress AUTOSAR-M3_3_2-a "False positive, this is not a member function and thus cannot be static."
1287
1288// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
1289/// @brief Check if the variant holds a value of a given alternative type.
1290/// @tparam T the alternative type to check
1291/// @tparam Ts the types that the variant can hold
1292/// @param var the variant to check
1293/// @return true if and only if the variant holds a value of type T.
1294/// @return false otherwise.
1295/// @pre Ill-formed if @c T does not appear exactly once in @c Ts .
1296template <class T, class... Ts>
1297constexpr auto holds_alternative(variant<Ts...> const& var) noexcept -> bool {
1298 return var.index() == base::index_of_v<T, Ts...>;
1299}
1300// parasoft-end-suppress AUTOSAR-M3_3_2-a
1301
1302/// @brief Attempt to get a pointer to the object held by the variant by index.
1303/// @tparam Index Index of the type in @c Ts to get
1304/// @tparam Ts the types that the variant can hold
1305/// @param ptr A pointer to the variant to access
1306/// @return A pointer to the held object if the variant holds a value of the type, else @c nullptr .
1307template <std::size_t Index, typename... Ts>
1308inline constexpr auto get_if(variant<Ts...>* ptr) noexcept
1310 using type = variant_alternative_t<Index, variant<Ts...>>;
1311 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1312 static_assert(!std::is_void<type>::value, "T should not be void");
1313 if ((ptr != nullptr) && (ptr->index() == Index)) {
1314 return std::addressof(ptr->template get<Index>());
1315 }
1316 return nullptr;
1317}
1318
1319/// @brief Attempt to get a pointer to the object held by the variant by index.
1320/// @tparam Index Index of the type in @c Ts to get
1321/// @tparam Ts the types that the variant can hold
1322/// @param ptr A pointer to the variant to access
1323/// @return A pointer to the held object if the variant holds a value of the type, else @c nullptr .
1324template <std::size_t Index, typename... Ts>
1325inline constexpr auto get_if(variant<Ts...> const* ptr) noexcept
1326 -> std::add_pointer_t<variant_alternative_t<Index, variant<Ts...>> const> {
1327 using type = variant_alternative_t<Index, variant<Ts...>>;
1328 static_assert(Index < sizeof...(Ts), "The index should be in [0, number of alternatives)");
1329 static_assert(!std::is_void<type>::value, "T should not be void");
1330 if ((ptr != nullptr) && (ptr->index() == Index)) {
1331 return std::addressof(ptr->template get<Index>());
1332 }
1333 return nullptr;
1334}
1335
1336/// @brief Attempt to get a pointer to the object held by the variant by type.
1337/// @tparam T Type in @c Ts to get
1338/// @tparam Ts the types that the variant can hold
1339/// The variant to access is passed as @c ptr.
1340/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1341/// @param ptr A pointer to the variant to access
1342/// @endcond
1343/// @return A pointer to the held object if the variant holds a value of the type, else @c nullptr .
1344template <typename T, typename... Ts>
1345inline constexpr auto get_if(variant<Ts...>* ptr) noexcept -> std::add_pointer_t<T> {
1346 static_assert(!std::is_void<T>::value, "T should not be void");
1347 return get_if<base::index_of_v<T, Ts...>>(ptr);
1348}
1349/// @brief Attempt to get a pointer to the object held by the variant by type.
1350/// @tparam T Type in @c Ts to get
1351/// @tparam Ts the types that the variant can hold
1352/// The variant to access is passed as @c ptr.
1353/// @cond CPPTEST_PARAMETER_DOCUMENTATION
1354/// @param ptr A pointer to the variant to access
1355/// @endcond
1356/// @return A pointer to the held object if the variant holds a value of the type, else @c nullptr .
1357template <typename T, typename... Ts>
1358inline constexpr auto get_if(variant<Ts...> const* ptr) noexcept -> std::add_pointer_t<T const> {
1359 static_assert(!std::is_void<T>::value, "T should not be void");
1360 return get_if<base::index_of_v<T, Ts...>>(ptr);
1361}
1362
1363} // namespace base
1364} // namespace arene
1365
1366#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_VARIANT_VARIANT_HPP_
Provides a backport of std::void_t.
constexpr auto data() noexcept -> T *
Obtain a pointer to the first element of the array.
Definition array.hpp:145
backport of std::in_place_index_t<size_t> from C++17
Definition in_place.hpp:54
A special dummy class that cannot be constructed. It can be used as a substitute argument for copy/mo...
Definition non_constructible_dummy.hpp:16
Forward declaration of variant.
Definition variant.hpp:540
~variant()
Calls the destructor of the alternative type.
Definition variant.hpp:1104
auto get() const &&noexcept(!detail::are_exceptions_enabled_v) -> T const &&
Get by type.
Definition variant.hpp:1040
constexpr auto valueless_by_exception() const noexcept -> bool
Check if the variant is valueless.
Definition variant.hpp:1101
variant(dummy_move_ctor_source &&other)=delete
Move Constructor form another variant.
auto get() &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &&
Get the object stored in this variant by the index.
Definition variant.hpp:1078
auto operator=(copy_assignment_arg const &rhs) noexcept(base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_copy_assignable< Ts >::value... >) -> variant &
Copy Assignment operator from another variant of the same alternative types.
Definition variant.hpp:797
variant(copy_ctor_arg const &other) noexcept(arene::base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... >)
Copy Constructor from another variant of the same alternative types.
Definition variant.hpp:667
variant(T &&value) noexcept(std::is_nothrow_constructible< Tj, T >::value)
Constructor that initializes this variant from one of its alternative types.
Definition variant.hpp:732
variant(in_place_index_t< I >, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, Args... >::value)
Constructor that initializes this variant from a specified alternative index.
Definition variant.hpp:754
variant() noexcept(std::is_nothrow_default_constructible< T0 >::value)
Default constructor, which initializes the variant to a default constructed instance of its 0'th alte...
Definition variant.hpp:651
auto operator=(dummy_copy_assign_source const &rhs) -> variant &=delete
Copy Assignment operator from another variant of the same alternative types.
auto emplace(Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, Args... >::value) -> nth_type< I > &
Emplaces a value into the variant by index.
Definition variant.hpp:921
auto get() const &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &&
Get the object stored in this variant by the index.
Definition variant.hpp:1090
variant(move_ctor_arg &&other) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... >)
Move Constructor from another variant.
Definition variant.hpp:692
auto get() &&noexcept(!detail::are_exceptions_enabled_v) -> T &&
Get by type.
Definition variant.hpp:1029
variant(in_place_index_t< I >, std::initializer_list< U > init_list, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, std::initializer_list< U > &, Args... >::value)
Constructor that initializes this variant from a specified alternative index via an initializer list.
Definition variant.hpp:781
auto emplace(std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< T, std::initializer_list< U >, Args... >::value) -> T &
Emplaces a value into the variant by type.
Definition variant.hpp:986
auto get() &noexcept(!detail::are_exceptions_enabled_v) -> T &
Get by type.
Definition variant.hpp:1006
auto emplace(Args &&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) -> T &
Emplaces a value into the variant by type.
Definition variant.hpp:966
friend auto operator<(variant const &lhs, variant const &rhs) noexcept -> bool
Less-than comparison.
Definition variant.hpp:890
auto emplace(std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, std::initializer_list< U >, Args... >::value) -> nth_type< I > &
Emplaces a value into the variant by index.
Definition variant.hpp:944
friend auto operator==(variant const &lhs, variant const &rhs) noexcept -> bool
Equality comparison.
Definition variant.hpp:866
variant(dummy_copy_ctor_source const &other)=delete
Copy Constructor from another variant of the same alternative types.
auto get() &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &
Get the object stored in this variant by the index.
Definition variant.hpp:1054
auto operator=(dummy_move_assign_source &&rhs) -> variant &=delete
Move Assignment operator from another variant of the same alternative types.
auto get() const &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &
Get the object stored in this variant by the index.
Definition variant.hpp:1066
auto operator=(move_assignment_arg &&rhs) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_move_assignable< Ts >::value... >) -> variant &
Move Assignment operator from another variant of the same alternative types.
Definition variant.hpp:828
auto get() const &noexcept(!detail::are_exceptions_enabled_v) -> T const &
Get by type.
Definition variant.hpp:1017
auto index() const noexcept -> std::size_t
Get the index of the alternative type that this variant currently holds.
Definition variant.hpp:908
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
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 and is noexcept...
Definition is_constructible.hpp:84
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
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
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.
typename at< L0, Index >::type at_t
Get the n-th element from a type list.
Definition at.hpp:113
Definition accumulate.hpp:37
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
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 std::size_t variant_npos
Special value used as the return value of variant::index() when variant::valueless_by_exception() is ...
Definition traits.hpp:34
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
void void_t
An alias for void that accepts any number of type parameters, used for substitution checks.
Definition substitution_succeeds.hpp:17
constexpr auto get_if(variant< Ts... > *ptr) noexcept -> std::add_pointer_t< variant_alternative_t< Index, variant< Ts... > > >
Attempt to get a pointer to the object held by the variant by index.
Definition variant.hpp:1308
typename variant_alternative< I, T >::type variant_alternative_t
Helper type of variant_size variant_alternative.
Definition traits.hpp:84
constexpr bool all_of_v
Trait which tests if every value in a parameter pack is true .
constexpr auto holds_alternative(variant< Ts... > const &var) noexcept -> bool
Check if the variant holds a value of a given alternative type.
Definition variant.hpp:1297
constexpr auto get(array< T, N > &arr) noexcept -> T &
Get a reference to the element at the specified index in the array.
Definition array.hpp:885
constexpr auto index_of_v
Template meta-function for querying the 0-based index of 'T' within a list of types,...
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
Definition accumulate.hpp:36
STL namespace.
constexpr auto max(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the largest value in the std::initializer_list.
Definition max_initializer_list_overload.hpp:49
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
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
typename add_pointer< Type >::type add_pointer_t
Adds a pointer to the given type.
Definition add_pointer.hpp:62
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
an empty struct that is a list of types - cheaper to instantiate than tuple
Definition type_list.hpp:19
Provides the all_of_v<bool...> trait.