UgMisc 0.2-128
Miscellaneous C++ header library
Loading...
Searching...
No Matches
xchg.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: © 2025 Larry Chips <larry@larrychips.net>
3 * SPDX-Licence-Identifier: MIT
4 */
5#ifndef UGMISC_XCHG_HPP
6#define UGMISC_XCHG_HPP
7
8#include "ugmisc/features.hpp"
9
10#ifdef UGMISC_HAVE_CONSTEXPR_EXCHANGE
11#include <algorithm>
12#endif
13#include <utility>
14
15
25
26
27
28
29namespace ugmisc {
30
31
32
33
41template<class T, class U=T>
42constexpr T xchg(T& object, U&& new_value) {
44 return std::exchange(object, std::forward<U>(new_value));
45 } else {
46 T ret{std::move(object)};
47 object = std::move(new_value);
48 return ret;
49 }
50}
51
52
53
54
55} /* ::ugmisc */
56
57#endif /* UGMISC_XCHG_HPP */
Feature detection.
static constexpr bool constexpr_swap_algorithms
Definition features.hpp:447
constexpr T xchg(T &object, U &&new_value)
Definition xchg.hpp:42