Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- // using std::vector
- #include <utility>
- // using std::swap
- namespace ns {
- struct Obj {
- /* some pseudo-data */
- std::vector<int> data;
- int x;
- /* define member function to do the actual swapping */
- void swap(Obj& b) {
- using std::swap;
- /* never call std::swap(a,b), always import std::swap with using,
- * then let the lookup rules figure out which swap specialization
- * to call
- */
- swap(this->data, b.data);
- swap(this->x, b.x);
- }
- };
- /* define swap in the same namespace as your class */
- void swap(Obj& a, Obj&b) {
- a.swap(b);
- }
- }
- /* specialize std::swap unless your class is a template */
- namespace std {
- template<>
- void swap<ns::Obj>(ns::Obj& a, ns::Obj& b) {
- a.swap(b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment