I overloaded swap function for my class as in this answer, but while sorting (std::sort
) compiler is still using std::swap
. I don't see any difference between my approach and the one stated in linked answer. Here's reproduction of my code:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
struct B
{
struct A
{
friend void swap(A & a, A & b)
{
std::swap(a.a, b.a);
std::cout << "my swap\n";
}
A(int _a) : a(_a) {}
bool operator<(const A & other) { return a < other.a; }
int a;
};
};
int main()
{
std::vector<B::A> v{1, 2, 3, 5, 4};
std::sort(std::begin(v), std::end(v));
}
Also executable example provided here.