58

An implementation of std::swap might look like this:

template <class T> void swap (T& a, T& b)
{
  T c(std::move(a)); a=std::move(b); b=std::move(c);
}
template <class T, size_t N> void swap (T (&a)[N], T (&b)[N])
{
  for (size_t i = 0; i<N; ++i) swap (a[i],b[i]);
}

An implementation std::exchange n3668 might look like this:

 template< typename T, typename U = T >
   T exchange( T & obj, U && new_val )
   {
     T old_val = std::move(obj);
     obj = std::forward<U>(new_val);
     return old_val;
   }

It says:

For primitive types, this is equivalent to the obvious implementation, while for more complex types, this definition

  • Avoids copying the old value when that type defines a move constructor
  • Accepts any type as the new value, taking advantage of any converting assignment operator
  • Avoids copying the new value if it's a temporary or moved.

I chose the name for symmetry with atomic_exchange, since they behave the same except for this function not being atomic.

n3746 also proposes a built-in swap operator that looks like this:

inline C& C::operator :=: (C&& y) &  { see below; return *this; } 
inline C& C::operator :=: (C& y)  &  { return *this :=: std::move(y); }

From what I gather, the proposals would like all three of these options to live side by side, rather than replacing each other. Why is it necessary to have three different ways to swap objects?

  • 5
    `std::exchange` doesn't swap, it works like an atomic `exchange_***` operation - except it's not atomic. It returns you the old value and you can do what you want with it (I guess you could actually reimplement `swap` in terms of `exchange`, but you're not forced to swap). As for the `:=:` operator, I *think* that would supersede `std::swap`, so if they will coexist it is only for backwards compatibility - but that's just a feeling, I haven't read the proposal. – Andy Prowl Dec 27 '13 at 21:30
  • A swap operator could be implicitly defined, and this implicit definition could make use of optimized swap operators of bases and members. – dyp Dec 27 '13 at 21:38
  • 2
    There's also some rationale in the proposal for the operator: "For example, it took a long time before we understood how to call the algorithm in the presence of type-specific versions. We thought that that problem was solved [using + ADL] but analogues keep popping up in new contexts, most recently while discussing `noexcept` behavior on the std-proposals reflector. We believe this issue is so important as to warrant an addition to the C++ core language, the swap operator." – dyp Dec 27 '13 at 21:43
  • By the look of it, `swap` is for *two* objects, while `exchange` is for *one* object and one value. – Kerrek SB Dec 27 '13 at 21:49
  • 14
    `exchange` is like a post-assignment ;) `exchange(i, i+1)` is a post-increment. – dyp Dec 27 '13 at 21:52
  • 2
    You forget parenthesis in your swap prototype: `template void swap (T (&a)[N], T (&b)[N])` – Jarod42 Dec 27 '13 at 22:28
  • 3
    @DyP: Good point. `exchange` is to `swap` as `next` is to `advance`. – Kerrek SB Dec 27 '13 at 22:30
  • @Jarod42: Sorry for the off-topic and out-of-place question, but why are those parentheses necessary? (I'm referring to `(&a)` in the array version of `swap`.) – yzt Dec 30 '13 at 00:38
  • 2
    @yzt: It is the way to pass arrays, `void f(T a[N])` decays into `void f(T *a)`. And `void f(T &a[N])` is illegal (parsed as array of reference). – Jarod42 Dec 30 '13 at 01:28

2 Answers2

67

std::swap vs std::exchange

swap(x, y) and exchange(x, y) are not the same thing. exchange(x, y) never assigns a new value to y. You could do so if you use it like this: y = exchange(x, y). But that isn't the main use case for exchange(x, y). N3668 includes the statement:

The benefit isn't huge, but neither is the specification cost.

(with regard to standardizing exchange).

N3668 was voted into the C++1y working draft at the Bristol meeting, April 2013. The meeting minutes indicate that there was some discussion about the best name for this function in the Library Working Group, and that ultimately there was no objection to putting it up for a formal vote in full committee. The formal vote was strongly in favor of putting it into the working draft, but not unanimous.

Bottom line: exchange is a minor utility, does not compete with swap(x, y), and has far fewer use cases.

std::swap vs swap operator

N3553, a previous revision to N3746, was discussed in the Evolution Working Group at the April 2013 meeting in Bristol. The meeting minutes acknowledge "annoying ADL problems" with std::swap(x, y), but conclude that a swap operator would not address those problems. Because of backwards compatibility, the EWG also believed that if accepted, std::swap and the swap operator would forever co-exist. The EWG decided in Bristol not to proceed with N3553.

The Sep. 2013 Chicago EWG meeting minutes make no mention of N3746. I was not present at that meeting but presume that the EWG declined to look at N3746 because of its previous decision in Bristol on N3553.

Bottom line: The C++ committee does not appear to be moving forward with a swap operator at this time.

Update: Can std::exchange be faster than std::swap?

Preview: No. At best exchange will be just as fast as swap. At worst, it can be slower.

Consider a test like this:

using T = int;

void
test_swap(T& x, T& y)
{
    using std::swap;
    swap(x, y);
}

void
test_exchange(T& x, T& y)
{
    y = std::exchange(x, std::move(y));
}

Which generates faster code?

Using clang -O3, they both generate identical code (except for the mangled names of the functions):

__Z9test_swapRiS_:                      ## @_Z9test_swapRiS_
    .cfi_startproc
## BB#0:                                ## %entry
    pushq   %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    movl    (%rdi), %eax
    movl    (%rsi), %ecx
    movl    %ecx, (%rdi)
    movl    %eax, (%rsi)
    popq    %rbp
    retq
    .cfi_endproc

For some arbitrary type X, which does not have a specialized swap function, both tests will generate one call to X(X&&) (assuming move members exist for X), and two calls X& operator=(X&&):

test_swap

__Z9test_swapR1XS0_:                    ## @_Z9test_swapR1XS0_
    .cfi_startproc
## BB#0:                                ## %entry
    pushq   %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    pushq   %r15
    pushq   %r14
    pushq   %rbx
    pushq   %rax
Ltmp3:
    .cfi_offset %rbx, -40
Ltmp4:
    .cfi_offset %r14, -32
Ltmp5:
    .cfi_offset %r15, -24
    movq    %rsi, %r14
    movq    %rdi, %rbx
    leaq    -32(%rbp), %r15
    movq    %r15, %rdi
    movq    %rbx, %rsi
    callq   __ZN1XC1EOS_
    movq    %rbx, %rdi
    movq    %r14, %rsi
    callq   __ZN1XaSEOS_
    movq    %r14, %rdi
    movq    %r15, %rsi
    callq   __ZN1XaSEOS_
    addq    $8, %rsp
    popq    %rbx
    popq    %r14
    popq    %r15
    popq    %rbp
    retq
    .cfi_endproc

test_exchange

    .globl  __Z13test_exchangeR1XS0_
    .align  4, 0x90
__Z13test_exchangeR1XS0_:               ## @_Z13test_exchangeR1XS0_
    .cfi_startproc
## BB#0:                                ## %entry
    pushq   %rbp
Ltmp6:
    .cfi_def_cfa_offset 16
Ltmp7:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp8:
    .cfi_def_cfa_register %rbp
    pushq   %r14
    pushq   %rbx
    subq    $16, %rsp
Ltmp9:
    .cfi_offset %rbx, -32
Ltmp10:
    .cfi_offset %r14, -24
    movq    %rsi, %r14
    movq    %rdi, %rbx
    leaq    -24(%rbp), %rdi
    movq    %rbx, %rsi
    callq   __ZN1XC1EOS_
    movq    %rbx, %rdi
    movq    %r14, %rsi
    callq   __ZN1XaSEOS_
    leaq    -32(%rbp), %rsi
    movq    %r14, %rdi
    callq   __ZN1XaSEOS_
    addq    $16, %rsp
    popq    %rbx
    popq    %r14
    popq    %rbp
    retq
    .cfi_endproc

Again nearly the same code.

But for types that have an optimized swap, test_swap is likely to generate far superior code. Consider:

using T = std::string;

(using libc++)

test_swap

    .globl  __Z9test_swapRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_
    .align  4, 0x90
__Z9test_swapRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_: ## @_Z9test_swapRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_
    .cfi_startproc
## BB#0:                                ## %entry
    pushq   %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    movq    16(%rdi), %rax
    movq    %rax, -8(%rbp)
    movq    (%rdi), %rax
    movq    8(%rdi), %rcx
    movq    %rcx, -16(%rbp)
    movq    %rax, -24(%rbp)
    movq    16(%rsi), %rax
    movq    %rax, 16(%rdi)
    movq    (%rsi), %rax
    movq    8(%rsi), %rcx
    movq    %rcx, 8(%rdi)
    movq    %rax, (%rdi)
    movq    -8(%rbp), %rax
    movq    %rax, 16(%rsi)
    movq    -24(%rbp), %rax
    movq    -16(%rbp), %rcx
    movq    %rcx, 8(%rsi)
    movq    %rax, (%rsi)
    popq    %rbp
    retq
    .cfi_endproc

test_exchange

    .globl  __Z13test_exchangeRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_
    .align  4, 0x90
__Z13test_exchangeRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_: ## @_Z13test_exchangeRNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEES6_
Lfunc_begin0:
    .cfi_startproc
    .cfi_personality 155, ___gxx_personality_v0
    .cfi_lsda 16, Lexception0
## BB#0:                                ## %entry
    pushq   %rbp
Ltmp9:
    .cfi_def_cfa_offset 16
Ltmp10:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp11:
    .cfi_def_cfa_register %rbp
    pushq   %r14
    pushq   %rbx
    subq    $32, %rsp
Ltmp12:
    .cfi_offset %rbx, -32
Ltmp13:
    .cfi_offset %r14, -24
    movq    %rsi, %r14
    movq    %rdi, %rbx
    movq    16(%rbx), %rax
    movq    %rax, -32(%rbp)
    movq    (%rbx), %rax
    movq    8(%rbx), %rcx
    movq    %rcx, -40(%rbp)
    movq    %rax, -48(%rbp)
    movq    $0, 16(%rbx)
    movq    $0, 8(%rbx)
    movq    $0, (%rbx)
Ltmp3:
    xorl    %esi, %esi
                                        ## kill: RDI<def> RBX<kill>
    callq   __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7reserveEm
Ltmp4:
## BB#1:                                ## %_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE5clearEv.exit.i.i
    movq    16(%r14), %rax
    movq    %rax, 16(%rbx)
    movq    (%r14), %rax
    movq    8(%r14), %rcx
    movq    %rcx, 8(%rbx)
    movq    %rax, (%rbx)
    movq    $0, 16(%r14)
    movq    $0, 8(%r14)
    movq    $0, (%r14)
    movw    $0, (%r14)
Ltmp6:
    xorl    %esi, %esi
    movq    %r14, %rdi
    callq   __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE7reserveEm
Ltmp7:
## BB#2:                                ## %_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEaSEOS5_.exit
    movq    -32(%rbp), %rax
    movq    %rax, 16(%r14)
    movq    -48(%rbp), %rax
    movq    -40(%rbp), %rcx
    movq    %rcx, 8(%r14)
    movq    %rax, (%r14)
    xorps   %xmm0, %xmm0
    movaps  %xmm0, -48(%rbp)
    movq    $0, -32(%rbp)
    leaq    -48(%rbp), %rdi
    callq   __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev
    addq    $32, %rsp
    popq    %rbx
    popq    %r14
    popq    %rbp
    retq
LBB1_3:                                 ## %terminate.lpad.i.i.i.i
Ltmp5:
    movq    %rax, %rdi
    callq   ___clang_call_terminate
LBB1_4:                                 ## %terminate.lpad.i.i.i
Ltmp8:
    movq    %rax, %rdi
    callq   ___clang_call_terminate
Lfunc_end0:
    .cfi_endproc
    .section    __TEXT,__gcc_except_tab
    .align  2
GCC_except_table1:
Lexception0:
    .byte   255                     ## @LPStart Encoding = omit
    .byte   155                     ## @TType Encoding = indirect pcrel sdata4
    .asciz  "\242\200\200"          ## @TType base offset
    .byte   3                       ## Call site Encoding = udata4
    .byte   26                      ## Call site table length
Lset0 = Ltmp3-Lfunc_begin0              ## >> Call Site 1 <<
    .long   Lset0
Lset1 = Ltmp4-Ltmp3                     ##   Call between Ltmp3 and Ltmp4
    .long   Lset1
Lset2 = Ltmp5-Lfunc_begin0              ##     jumps to Ltmp5
    .long   Lset2
    .byte   1                       ##   On action: 1
Lset3 = Ltmp6-Lfunc_begin0              ## >> Call Site 2 <<
    .long   Lset3
Lset4 = Ltmp7-Ltmp6                     ##   Call between Ltmp6 and Ltmp7
    .long   Lset4
Lset5 = Ltmp8-Lfunc_begin0              ##     jumps to Ltmp8
    .long   Lset5
    .byte   1                       ##   On action: 1
    .byte   1                       ## >> Action Record 1 <<
                                        ##   Catch TypeInfo 1
    .byte   0                       ##   No further actions
                                        ## >> Catch TypeInfos <<
    .long   0                       ## TypeInfo 1
    .align  2

So in summary, never use std::exchange to perform a swap.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • 1
    What does "ADL" mean in _"The meeting minutes acknowledge 'annoying ADL problems' {with std::swap(x, y)}[..]"_ ? A quick web search wasn't helpful. - EDIT: ADL is an acronym for Argument-dependent lookup. https://en.cppreference.com/w/cpp/language/adl – J. Coenen May 01 '20 at 09:31
  • I think your comparison does not take advantage of the main advantage of std::exchange: building y in-place. – Julien Sep 29 '20 at 19:25
  • If you are referring to the idiom of using `exchange` to construct a data member in a move constructor, while giving the source a moved-from value, I agree that is a good use case for `exchange`. I don't say never use `exchange`. I say never use it in place of `swap`. Many types have a customized `swap` that is more efficient than the generic `swap` or `exchange`. But types don't have a customized `exchange`. This is because `swap` is a documented "customization point", and so people write their code so that it can pick up that customization if it exists. – Howard Hinnant Sep 29 '20 at 19:55
  • Now with [C++20 and CPOs](/questions/53495848/what-are-customization-point-objects-and-how-to-use-them), those '*annoying ADL-problems*' are finally a thing of the past there. – Deduplicator Nov 06 '20 at 17:32
  • Once the returned value is used, out-of-ctor use should also be idiomatic and preferable to `swap`, e.g. for an lvalue `m` of some associative container type `M`: `m.emplace(std::exchange(m, M(m.get_allocator())))`. Also better code here in general, although code generation for move assignments of allocator-aware containers is often not *that* ideal. – FrankHB Feb 17 '21 at 06:25
7

Short answer: it isn't necessary, but it's useful.

Long answer:

One of the largest possible markets for C++ is scientific computations and engineering computations, which is dominated in many ways by Fortran. Fortran isn't exactly pleasant to program in, but generates superior results because of various numerical optimizations it is capable of. This was one of the major reasons behind the development of expression templates, which allowed libraries like Blitz++ to develop near-Fortran levels of speed (at the cost of long compile times and cryptic error messages).

Move semantics and expression templates were developed to speed up certain areas of C++, mostly by eliminating unnecessary copies and temporary values. In the case of move semantics, this drastically increased the speed of numerical computations at basically no cost to the end user; once they were supported and default move semantics were added to objects, many common uses in numerics became faster, simply by allowing already present libraries to stop doing full copies on common operations. Due to the dramatic success of move semantics, other areas of the language, traditionally dominated by idioms such as copy-and-swap, are being viewed in a new light, and standardized. std::array is an example of one such strength reduction; where as previously most standard writers would have said "use vectors, they do everything you want and who cares if they are slow", now the call is for more specialized and specific containers, such as the static std::array.

So why swap?

If you look at boost::swap you'll understand why we have a need for the new swap operator: Argument Dependent Lookup is difficult to encapsulate and use correctly, and results in an explosion of needed functions, where as the basic idea of just giving a swap member function is quite simple. Having an operator that can do it, and providing a default swap operator that can then be used for a default Copy And Swap is an enormous performance boost.

Why? Because std::swap is defined in terms of MoveConstructible and MoveAssignable in C++11 (formerly copy construction and copy assignment, in C++98); this requires three moves, and a temporary (much faster than the full copies needed in C++98). This is generic, and quite fast, but not quite as fast as a custom swap (which can be 2-3x faster, by removing the temporary and one move in many cases). std::swap also depends on the type being nothrow-move-constructible and nothrow-move-assignable; it is conceivable to think of a class which is not, but which could provide exception guarantees on a custom swap, thus avoiding undefined behavior.

ADL and std::swap can interact very nicely, but the syntax is somewhat odd; you add

using std::swap;

to your function calling swap, and provide a free friend function as a swap specialization. Replacing this strange implicit ADL corner case with an explicit operator would be easier on the eyes, but as noted, it seems to be dead on arrival.

Exchange is a very similar beast

By using std::move in exchange, a full copy is no longer necessary. By using a universal reference for new_val, the new value can be perfectly forwarded or moved directly into it's new spot. In theory, exchange can run with absolutely zero copies, just two moves.

In Summary

Why is it necessary? Because it's fast and imposes no cost to end users, and expands C++ as a useful alternative to Fortran in scientific computing.

Alice
  • 3,958
  • 2
  • 24
  • 28