#include <iostream> #include <algorithm> #include <boost/array.hpp> #include <algorithm> #include <string> #include <vector> using namespace std; struct A { enum { SIZE = 3 }; A() { cout << "Constructor." << endl; p = new int[SIZE]; } A(const A &other) { cout << "Copy Constructor." << endl; p = new int[SIZE]; for (int i = 0; i < SIZE; ++i) p[i] = other.p[i]; } A(A &&other) { cout << "Move Constructor." << endl; p = other.p; other.p = 0; } A& operator=(const A &other) { cout << "Operator=." << endl; for (int i = 0; i < SIZE; ++i) p[i] = other.p[i]; return *this; } A& operator=(A &&other) { cout << "&& Operator=." << endl; p = other.p; other.p = 0; return *this; } ~A() { cout << "Destructor." << endl; delete[] p; } int *p; }; template <class T> void MySwap(T &a, T &b) { T tmp(move(a)); a = move(b); b = move(tmp); } template <class T> void MySwap2(T &a, T &b) { T tmp = a; a = b; b = tmp; } int main() { A s1, s2; for (int i = 0; i < A::SIZE; ++i) s1.p[i] = i, s2.p[i] = 100 + i; cout << endl; MySwap(s1, s2); cout << endl; //MySwap2(s1, s2); cout << endl; for (int i = 0; i < A::SIZE; ++i) cout << s1.p[i] << ' '; cout << endl; for (int i = 0; i < A::SIZE; ++i) cout << s2.p[i] << ' '; cout << endl; system("pause"); return 0; }