I'm studying functional operators and can't understand why code snippets like this compile:
class Pocket
{
int value;
public:
Pocket(int value) :value(value) {}
int getValue() const
{
return value;
}
operator int() const
{
return value;
}
bool operator<(const Pocket & _Right) const
{
return value < _Right.value;
}
};
int main() {
Pocket mynumbers1[] = { 3, 9, 2, -4, 4 };
Pocket mynumbers2[] = { 3, 9, 5, 0, 4 };
vector<Pocket> v1(mynumbers1, mynumbers1 + 5);
vector<Pocket> v2(mynumbers2, mynumbers2 + 5);
vector<Pocket> v3(5, 0);
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), minus<Pocket>());
return 0;
}
My reasoning is that when you call the minus<Pocket> operator inside transform, the whole thing would reduce to a subtraction between two Pockets. As you can see, the class doesn't defines a proper operator-, only an operator<, which should be useless in this case, and a conversion to int, which shouldn't even be considered since both arguments are of the same type.
So what am I missing?
minus()using the explicit cast operator ofPocket.int, which shouldn't even be considered since both arguments are of the same type" This statement is very unclear, can you elaborate more please why you think this should apply?operator intfrom thePocketclass - you will see that the code no longer compiles.