tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
exchange.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_UTILITY_EXCHANGE_HPP
5#define TETL_UTILITY_EXCHANGE_HPP
6
7#include <etl/_type_traits/is_nothrow_move_assignable.hpp>
8#include <etl/_type_traits/is_nothrow_move_constructible.hpp>
9#include <etl/_utility/forward.hpp>
10#include <etl/_utility/move.hpp>
11
12namespace etl {
13
14/// \brief Replaces the value of obj with new_value and returns the old value of obj.
15/// \returns The old value of obj.
16template <typename T, typename U = T>
17[[nodiscard]] constexpr auto
19 -> T
20{
21 T oldValue = etl::move(obj);
22 obj = etl::forward<U>(newValue);
23 return oldValue;
24}
25
26} // namespace etl
27
28#endif // TETL_UTILITY_EXCHANGE_HPP
Definition adjacent_find.hpp:9
constexpr auto exchange(T &obj, U &&newValue) noexcept(etl::is_nothrow_move_constructible_v< T > and etl::is_nothrow_assignable_v< T &, U >) -> T
Replaces the value of obj with new_value and returns the old value of obj.
Definition exchange.hpp:18