My Project
Functions
exchange.hpp File Reference
#include "forward.hpp"
#include "move.hpp"

Go to the source code of this file.

Functions

template<typename T , typename U = T>
constexpr T bsl::exchange (T &obj, U &&new_value) noexcept(false)
 Replaces the value of obj with new_value and returns the old value of obj. More...
 

Detailed Description

Function Documentation

◆ exchange()

template<typename T , typename U = T>
constexpr T bsl::exchange ( T &  obj,
U &&  new_value 
)
noexcept

Replaces the value of obj with new_value and returns the old value of obj.

#include <bsl/exchange.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_exchange_overview() noexcept
{
bsl::int32 const val1{23};
bsl::int32 const val2{42};
bsl::int32 var1{val1};
bsl::int32 var2{val2};
var2 = bsl::exchange(var1, var2);
if (val2 == var1) {
bsl::print() << "success\n";
}
if (val1 == var2) {
bsl::print() << "success\n";
}
}
}
Template Parameters
Tthe type that defines the obj being exchanged
Uthe type that defines the new value for obj
Parameters
objthe object whose value will be set to new_value
new_valuethe new value to set the obj to
Returns
returns the old value of obj prior to the exchange
Exceptions
throwsif obj's move constructor or obj's copy/move assignment throws