While answering this question I came across an interesting difference in the overload resolution of rvalue to reference between member and non-member operators.
Given two non-member operators, one passing the left parameter as const and one as non-const, both GCC 4.4.3 and MSVC2010 choose the const version when called with an rvalue.
However, given two member operators, one const and one non-const, both compilers choose the non-const version.
I assume that both compilers are complying with the standard on this, so I am curious as to why there is this discrepancy between const overload resolution between members and non-members. Please enlighten me :)
Here's some code to illustrate the difference:
#include <iostream>
class C {
public:
C(int i) { }
/*
C operator<<(C const &rhs) {
std::cout << "member non-const" << std::endl;
return *this;
}
C operator<<(C const &rhs) const {
std::cout << "member const" << std::endl;
return *this;
}
//*/
};
C operator<<(C &lhs, C const &rhs) {
std::cout << "non-member non-const" << std::endl;
return lhs;
}
C operator<<(C const &lhs, C const &rhs) {
std::cout << "non-member const" << std::endl;
return lhs;
}
int main() {
// Will print:
// "non-member const" when member operators are commented out
// "member non-const" when members are uncommented
C(5) << 6;
}