this answerのように私のクラスのスワップ関数がオーバーロードされましたが、ソート(std::sort
)コンパイラはまだstd::swap
を使用しています。私は私のアプローチとリンクされた答えに書かれているものとの間に違いは見られません。ここに私のコードの再現です:スワップ関数のオーバーロードを無視する
#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));
}
hereも実行可能な例です。
[これ](http://eel.is/c++draft/utility.requirements#swappable.requirements-3.2)関連するかもしれない:_ "スワップ要件を持つライブラリコンポーネントが含まれているかどうかを指定されていませんヘッダを使用して、適切な評価コンテキストを確保してください。 "_ –
[あなたのスワップ機能は' std'名前空間の内側からでも正しく呼び出されているようです。](https://wandbox.org/permlink/x6IV0XUtJaPXYPXW)。たぶん、 'std :: sort'では利用されていないかもしれません。 – VTT
ちょうど[パス](http://en.cppreference.com/w/cpp/algorithm/sort)関数オブジェクト::スワップはstdし、それを行うこと。 – Vorac