So, I was trying to come up with a solution for When does x==x+2 in C++ on codegolf, and came up with this snippet only to realize that I don’t know how it works. I’m not sure why both of these conditions evaluate to true.
Does anyone know if the line labeled line: is true because x==&x or because x+2 is evaluated before the left-hand side of ==?
#include <iostream>
#include <vector>
std::vector<int>& operator+ ( std::vector<int> &v, int val )
{
v.push_back(val);
return v;
}
int main()
{
std::vector<int> x;
std::vector<int> y = x + 2; // y is a copy of x, and x is [2]
// how are both of these are true?
std::cout << (x==y) << "\n"; // value comparison [2]==[2]
line:
std::cout << (x==x+2) << "\n"; // reference comparison? &x == &(x+2)
// not sure if this is relevant
std::cout << (x+2==x) << "\n"; // also true
return 0;
}
It seems–since vectors appear to be compared by value–that if x were evaluated before x+2, then x wouldn’t be equal to x+2 (by value). I’m probably missing something obvious. Thanks in advance.
For the standard containers,
operator==is overloaded asstd::equal. This in turn works on iterators and applies comparison by dereferncing, as in*it1 == *it2. Therefore, no copies are required.The expression
x == x + 2is the same asoperator==(x, x + 2). Both operands are evaluated before the function call, and sincex + 2modifiesx, both operands are the same. Thus the equality holds.The surprise is the result of your unconventional design choice in overloading the
+-operator. This is generally poor practice and a taboo in any collaborative project. If you absolutely must overload operators, then only if they behave as expected, follow established semantics and are not surprising. The usual behaviour of the+-operator is to return a new object, by value, and leave the operand unaffected. Like so: