Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
swap.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_SWAP_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_SWAP_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <utility>
12// IWYU pragma: friend "stdlib_detail/.*"
13
19
20namespace std {
21
22// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
23
24// parasoft-begin-suppress AUTOSAR-A8_4_8-a "False positive: 'lhs' and 'rhs' are in-out parameters, not output
25// parameters"
26
27/// @brief swaps the values of two objects
28///
29/// @tparam T type of the objects to swap
30/// @param lhs object to swap
31/// @param rhs object to swap
32template <class T, arene::base::constraints<std::enable_if_t<arene::base::is_default_swappable_v<T>>> = nullptr>
33constexpr auto swap(T& lhs, T& rhs) noexcept(arene::base::is_nothrow_default_swappable_v<T>) -> void {
34 auto temp = std::move(lhs);
35 lhs = std::move(rhs);
36 rhs = std::move(temp);
37}
38
39// parasoft-end-suppress AUTOSAR-A8_4_8-a
40
41/// @brief swaps the contents of two arrays
42///
43/// @tparam T type of the objects in the arrays to swap
44/// @param lhs array to swap
45/// @param rhs array to swap
46template <class T, std::size_t N, arene::base::constraints<std::enable_if_t<arene::base::is_swappable_v<T>>> = nullptr>
47// NOLINTNEXTLINE(hicpp-avoid-c-arrays) explicitly providing c-array support
48constexpr auto swap(T (&lhs)[N], T (&rhs)[N]) noexcept(arene::base::is_nothrow_swappable_v<T>) -> void {
49 for (std::size_t idx{}; idx < N; ++idx) {
50 swap(lhs[idx], rhs[idx]);
51 }
52}
53
54// parasoft-end-suppress AUTOSAR-M3_3_2-a
55
56} // namespace std
57
58#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_SWAP_HPP_
Public export header for the constraints subpackage.
constexpr bool is_nothrow_swappable_v
Trait query indicating if a type can be noexcept-swapped.
constexpr bool is_nothrow_default_swappable_v
Trait query if a type can be noexcept swapped the default implementation.
STL namespace.
void swap(pair< T1, T2 > &lhs, pair< T1, T2 > &rhs) noexcept(arene::base::is_nothrow_swappable_v< T1 > &&arene::base::is_nothrow_swappable_v< T2 >)
Swap a pair with another instance of the same type. Swaps lhs.first with rhs.first and swaps lhs....
Definition pair.hpp:448
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