jeudi 24 septembre 2015

std::swap does not call my custom swap implementation c++11

I have code like this:

#include <utility>
#include <iostream>

struct thing {
    void swap(thing & other){
        std::cout << "swap method" << std::endl;
    }
};

void swap(thing & a, thing & b) {
    std::cout << "swap function" << std::endl;

    a.swap(b);
}

struct another{
    thing a;
};


int main(int argc, char** argv){
    another a, b;

    std::swap(a, b);
}

If executed, it prints nothing - e.g. it does not use my "custom" swap.

I read I should NOT std::swap specialization.

Do I need to do custom swap for class another, or I am missing something?

Aucun commentaire:

Enregistrer un commentaire