为什么std :: swap可以正确交换自定义对象?

时间:2016-09-24 10:01:26

标签: c++ c++11

我已经编写了以下代码作为在c ++中正确实现swap函数的示例。

#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;
}

输出结果为:

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

当我打电话给不合格的swap时,每件事都按预期工作。但是,当我使用自定义对象调用std::swap时,我期待编译错误。为什么std::swap函数能够正确交换我的自定义对象?

1 个答案:

答案 0 :(得分:2)

Swapable是MoveAssignable和MoveConstructible,然后std::swap可以很好地使用它。

  

类型要求

     

这意味着std::map可以使用Swapable提供的move-assignment和move-construct操作来完成工作。类Swapable符合要求,其中包含隐式声明的移动构造函数和移动赋值运算符(以及隐式声明的复制构造函数和复制赋值运算符)。