メンバーとして定数参照を持つクラスでコピー アンド スワップ イディオムを使用すると、上記のエラーが発生します。
コード例:
#include <iostream>
#include <functional>
using std::reference_wrapper;
class I_hold_reference;
void swap(I_hold_reference& first, I_hold_reference& second);
class I_hold_reference{
inline I_hold_reference(const int& number_reference) : my_reference(number_reference){}
friend void swap(I_hold_reference& first, I_hold_reference& second);
inline I_hold_reference& operator=(I_hold_reference other){
swap(*this, other);
return *this;
}
inline I_hold_reference& operator=(I_hold_reference&& other){
swap(*this, other);
return *this;
}
private:
reference_wrapper<const int> my_reference;
};
void swap(I_hold_reference& first, I_hold_reference& second){
first = I_hold_reference(second.my_reference); //error: use of overloaded operator '=' is ambiguous (with operand types 'I_hold_reference' and 'I_hold_reference')
}
Copy 代入演算子が、引数を値ではなく参照で受け取るように変更されている場合、エラーが修正されます。
inline I_hold_reference& operator=(I_hold_reference& other){ ... }
これでエラーが修正されるのはなぜですか? 考えられる意味の 1 つは、リンクされた質問で参照されている重要な最適化の可能性が失われていることです。それは参考文献に当てはまりましたか?この変更の他の影響は何ですか?
この演算子に依存するコードベースがあり、他のメンバーは存在せず、言及された参照のみが存在します。コードベースをこの変更に何らかの形で適応させる必要はありますか、それとも現状のままで安全ですか?