I was trying to define a unary operator+
for a certain type. I was hoping this operator could be used only within the scope of a class, but even if that's not possible, I'm still confused by this:
struct X {};
struct Foo {
friend const char* operator+(X x) { return "hi"; }
void foo() {
+X{}; // error: invalid argument type 'X' to unary expression
}
};
int main() {
+X{}; // error: invalid argument type 'X' to unary expression
return 0;
}
If the operator isn't callable inside the class, and it's not callable outside the class, is it callable at all? Where did it go?
(Note that +(X{})
behaves the same way, and if the operator+
is moved outside of struct Foo
, both calls are valid.)