Giuseppe Pes Giuseppe Pes - 2 months ago 14
C++ Question

Why can std::swap correctly swap custom objects?

I've written the following code as an example of a correct implementation of a

swap
function in c++.

#include <iostream>

class Complex
{
int a;
int b;
friend std::ostream& operator<<(std::ostream&, const Complex&);
friend void swap(Complex& rhs, Complex& lhs) noexcept;
public:
Complex(): a(0), b(0) {};
Complex(int a, int b): a(a), b(b) {};
};

void swap(Complex& rhs, Complex& lhs) noexcept
{
using std::swap;
swap(rhs.a, lhs.a);
swap(rhs.b, lhs.b);
}

std::ostream& operator<<(std::ostream& os, const Complex& c)
{
os << c.a << c.b;
return os;
}

class Swapable
{
int a;
int b;
Complex complex;
friend std::ostream& operator<<(std::ostream&, const Swapable&);
friend void swap(Swapable&, Swapable&) noexcept;
public:
Swapable(): a(0), b(0) {};
Swapable(int a, int b): a(a), b(b), complex(a+1, b+1) {};
};

void swap(Swapable& rhs, Swapable& lhs) noexcept
{
std::cout << "Swapping" << std::endl;
using std::swap;
swap(rhs.a, lhs.a);
swap(rhs.b, lhs.b);
swap(rhs.complex, lhs.complex);
}

std::ostream& operator<<(std::ostream& os, const Swapable& s)
{
os << s.a << s.b << s.complex;
return os;
}

int main()
{
Swapable s_1(1,1);
Swapable s_2(2,2);

std::cout << "Before swap" << std::endl;
std::cout << s_1 << std::endl;
std::cout << s_2 << std::endl;

swap(s_1, s_2);

std::cout << "After swap" << std::endl;
std::cout << s_1 << std::endl;
std::cout << s_2 << std::endl;

std::swap(s_1, s_2); // It should fail. Shouldn't it?

std::cout << "Second swap" << std::endl;
std::cout << s_1 << std::endl;
std::cout << s_2 << std::endl;
}


The output is :

Before swap
1122
2233
Swapping
After swap
2233
1122
Second swap
1122
2233


Every thing works as expected when I call the unqualified
swap
. However, I was expecting a compile error when I call the
std::swap
with my custom object. Why is the
std::swap
function able to swap correctly my custom object?

Answer

Class Swapable is MoveAssignable and MoveConstructible, then std::swap could work well with it.

Type requirements

This implies std::map could do the work with the move-assignment and move-construct operation provided by Swapable. Class Swapable meets the requirements, there're implicitly-declared move constructor and move assignment operator, (and implicitly-declared copy constructor and copy assignment operator) for it.