While using the copy-and-swap idiom in a class that has constant references as members, the above error occurs.
Example code:
#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')
}
When the Copy assignment operator is being changed to take its argument by-reference, instead of by-value, the error is fixed.
inline I_hold_reference& operator=(I_hold_reference& other){ ... }
Why does this fix the error? One possible implication would be that the Important optimization possibility referenced in the linked question is lost. Was that ever true for references? What are the other implications of this change?
There is a codebase relying on this operator, no other members are present, only the mentioned references. Is there a need to adapt the codebase to this change somehow, or is it safe as-is?