Search code examples
c++templatesnamespacesswapargument-dependent-lookup

What's the point of iter_swap?


I was just wondering, why would anybody write this:

std::iter_swap(i, k);

instead of this?

std::swap(*i, *k);   // saved a few keystrokes!

Then I looked into the implementation of iter_swap, and of course it only uses swap instead of std::swap since we're already in namespace std, anyway. That leads me to the next question:

Why would anybody write this:

using std::swap;
swap(a, b);

instead of this?

std::iter_swap(&a, &b);   // saved an entire line of code!

Are there any important differences/issues I am overlooking here?


Solution

  • From the SGI docs (here):

    [1] Strictly speaking, iter_swap is redundant. It exists only for technical reasons: in some circumstances, some compilers have difficulty performing the type deduction required to interpret swap(*a, *b).