I overloaded the swap function for my class, as in this answer , but when sorting ( std::sort
), the compiler still uses std::swap
. I see no difference between my approach and what is indicated in the linked answer. Here's the 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 an executable example provided here .
Criss source
share