Namensräume
Varianten

std::exchange

Aus cppreference.com
< cpp‎ | utility

definiert in Header <utility>
template<class T, class U = T >
T exchange( T& obj, U&& new_value );
(seit C++14)
template<class T, class U = T >
constexpr T exchange( T& obj, U&& new_value );
template<class T, class U = T >
constexpr T exchange( T& obj, U&& new_value )noexcept(/* see below */);

Ersetzt den Wert von obj durch new_value und gibt den alten Wert von obj zurück.

Inhaltsverzeichnis

[Bearbeiten] Parameters

obj - Objekt, dessen Wert ersetzt werden soll
new_value - Wert, der obj zugewiesen werden soll
Type requirements
-
T must meet the requirements of . Es muß ebenfalls möglich sein, Objekte vom Typ U an Objekte des Typs T zuzuweisen

[Bearbeiten] Rückgabewerte

der alte Wert von obj

[Bearbeiten] Ausnahmen

(none)

(until C++23)
noexcept specification:  (seit C++11)
(since C++23)

[Bearbeiten] Mögliche Implementierungen

template<class T, class U = T>constexpr// since C++20 T exchange(T& obj, U&& new_value)noexcept(// since C++23std::is_nothrow_move_constructible<T>::value&&std::is_nothrow_assignable<T&, U>::value){ T old_value = std::move(obj); obj =std::forward<U>(new_value);return old_value;}

[Bearbeiten] Anmerkungen

std::exchange kann benutzt werden , um den Verschiebungszuweiseoperatoren und Verschiebekonstuktoren zu implementieren:

struct S {int n;   S(S&& other)noexcept: n{std::exchange(other.n, 0)}{}   S& operator=(S&& other)noexcept{if(this !=&other) n = std::exchange(other.n, 0);// move n, while leaving zero in other.nreturn*this;}};

Feature testing macro: __cpp_lib_exchange_function

[Bearbeiten] Beispiele

#include <iostream>#include <utility>#include <vector>#include <iterator>   class stream {public:   using flags_type =int;   public:   flags_type flags()const{return flags_;}   // Replaces flags_ by newf, and returns the old value. flags_type flags(flags_type newf){return std::exchange(flags_, newf);}   private:   flags_type flags_ =0;};   void f(){std::cout<<"f()";}   int main(){ stream s;   std::cout<< s.flags()<<'\n';std::cout<< s.flags(12)<<'\n';std::cout<< s.flags()<<"\n\n";   std::vector<int> v;   // Since the second template parameter has a default value, it is possible// to use a braced-init-list as second argument. The expression below// is equivalent to std::exchange(v, std::vector<int>{1,2,3,4});   std::exchange(v, {1,2,3,4});   std::copy(begin(v),end(v), std::ostream_iterator<int>(std::cout,", "));   std::cout<<"\n\n";   void(*fun)();   // the default value of template parameter also makes possible to use a// normal function as second argument. The expression below is equivalent to// std::exchange(fun, static_cast<void(*)()>(f)) std::exchange(fun,f); fun();   std::cout<<"\n\nFibonacci sequence: ";for(int a{0}, b{1}; a <100; a = std::exchange(b, a + b))std::cout<< a <<", ";std::cout<<"...\n";}

Output:

0 0 12   1, 2, 3, 4,   f()   Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

[Bearbeiten]See also

tauscht die Werte von zwei Objekten
Original:
swaps the values of two objects
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(Funktions-Template)[edit]
atomar ersetzt den Wert des atomaren Objekt mit nicht-elementare Argument und gibt den alten Wert des atomaren
Original:
atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(Funktions-Template)[edit]
close