Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
is_swappable.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_TYPE_TRAITS_IS_SWAPPABLE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_IS_SWAPPABLE_HPP_
7
8// IWYU pragma: private, include "arene/base/type_traits.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11#include "arene/base/constraints/substitution_succeeds.hpp"
12#include "arene/base/stdlib_choice/declval.hpp"
13#include "arene/base/stdlib_choice/is_move_assignable.hpp"
14#include "arene/base/stdlib_choice/is_move_constructible.hpp"
15#include "arene/base/stdlib_choice/is_same.hpp"
16#include "arene/base/stdlib_choice/remove_reference.hpp"
17
18namespace arene {
19namespace base {
20
21namespace swappable_detail {
22
23/// @brief Poison pill definition of @c swap
24///
25/// This is to ensure that @c use_adl_swap only looks for a function declaration and does not pick up some other global
26/// object named @c swap.
27// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
28template <class>
29auto swap() -> void = delete;
30// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
31
32/// @brief helper type trait for checking if ADL swap is valid, aliased to the result type of such an invocation
33template <typename T, typename U>
34using use_adl_swap = decltype(swap(std::declval<T>(), std::declval<U>()));
35} // namespace swappable_detail
36
37///
38/// @brief Trait query if a two types have an ADL-discoverable swap function.
39///
40/// @tparam T The first type to test.
41/// @tparam U The second type to test.
42/// @return If the expressions @c swap(std::declval<T>(),std::declval<U>()) and @c
43/// swap(std::declval<U>(),std::declval<T>()) are both well-formed in unevaluated context after using @c std::swap, the
44/// value equals true. Otherwise, the value is false.
45///
46template <typename T, typename U>
47extern constexpr bool is_adl_swappable_with_v = substitution_succeeds<swappable_detail::use_adl_swap, T, U> &&
49///
50/// @brief Trait query if a type has an ADL-discoverable swap function.
51///
52/// @tparam T The type to test.
53/// @return true if an unqualified call to @c swap(T&,T&) is well-formed.
54///
55template <typename T>
56extern constexpr bool is_adl_swappable_v = substitution_succeeds<swappable_detail::use_adl_swap, T&, T&>;
57
58/// @{
59///
60/// @brief Trait query if two types have a noexcept ADL-discoverable swap function.
61///
62/// @tparam T The first type to test.
63/// @tparam U The second type to test.
64/// @return If the expressions @c swap(std::declval<T>(),std::declval<U>()) and @c
65/// swap(std::declval<U>(),std::declval<T>()) are both well-formed in unevaluated context after using @c std::swap and
66/// are marked @c noexcept, the value equals true. Otherwise, the value is false.
67///
68template <typename T, typename U, bool = is_adl_swappable_with_v<T, U>>
69extern constexpr bool is_nothrow_adl_swappable_with_v = false;
70
71template <typename T, typename U>
72extern constexpr bool is_nothrow_adl_swappable_with_v<T, U, true> =
73 noexcept(swap(std::declval<T>(), std::declval<U>()))&& noexcept(swap(std::declval<U>(), std::declval<T>()));
74/// @}
75
76/// @brief Trait query if a type has a noexcept ADL-discoverable swap function.
77///
78/// @tparam T The type to test.
79/// @return true if an unqualified call to @c swap(T&,T&) is well-formed and it is marked @c noexcept .
80///
81template <typename T, bool = is_adl_swappable_v<T>>
82extern constexpr bool is_nothrow_adl_swappable_v = is_nothrow_adl_swappable_with_v<T&, T&>;
83
84///
85/// @brief Trait query if a type can be swapped via the default implementation.
86///
87/// @tparam T The type to test.
88/// @return true if @c std::is_move_assignable<T> and @c std::is_move_constructible<T> are true.
89///
90template <typename T>
91extern constexpr bool is_default_swappable_v =
93
94///
95/// @brief Trait query if a type can be noexcept swapped the default implementation.
96///
97/// @tparam T The type to test.
98/// @return true if @c std::is_nothrow_move_assignable<T> and @c std::is_nothrow_move_constructible<T> are true.
99///
100template <typename T>
101extern constexpr bool is_nothrow_default_swappable_v =
103
104///
105/// @brief Trait query indicating two types can be swapped
106///
107/// @tparam T the first type to test
108/// @tparam U the second type to test
109/// @return If the expressions @c swap(std::declval<T>(),std::declval<U>()) and @c
110/// swap(std::declval<U>(),std::declval<T>()) are both well-formed in unevaluated context after using @c std::swap, the
111/// value equals true. Otherwise, the value is false.
112///
113template <typename T, class U>
114extern constexpr bool is_swappable_with_v =
116
117///
118/// @brief Trait query indicating if a type can be swapped
119///
120/// @tparam T the type to test
121/// @return @c is_swappable_with_v<T&, T&>
122///
123template <typename T>
124extern constexpr bool is_swappable_v = is_swappable_with_v<T&, T&>;
125
126///
127/// @brief Trait query indicating if two types can be noexcept-swapped
128///
129/// @tparam T the first type to test
130/// @tparam U the second type to test
131/// @return If the expressions @c swap(std::declval<T>(),std::declval<U>()) and @c
132/// swap(std::declval<U>(),std::declval<T>()) are both well-formed in unevaluated context after using @c std::swap and
133/// are marked @c noexcept, the value equals true. Otherwise, the value is false.
134///
135template <typename T, typename U>
136extern constexpr bool is_nothrow_swappable_with_v =
139
140///
141/// @brief Trait query indicating if a type can be noexcept-swapped
142///
143/// @tparam T the type to test
144/// @return @c is_nothrow_swappable_with_v<T&, T&>
145///
146template <typename T>
147extern constexpr bool is_nothrow_swappable_v = is_nothrow_swappable_with_v<T&, T&>;
148
149} // namespace base
150} // namespace arene
151
152#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_IS_SWAPPABLE_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10