Consider the following code:
class C { public: int operator-(int x) { return 3-x; } }; class wrapper { public: operator C() { static C z; return z; } } wrap; int main() { return wrap-3; }
it gives this error in g ++:
test.cpp: In function 'int main()': test.cpp:17:17: error: no match for 'operator-' in 'wrap - 3'
The conversion operator seems to work because this version works:
class wrapper { public: operator int() { static int z=3; return z--; } } wrap; int main() { return wrap-3; }
operator- also works because this code compiles:
class C { public: int operator-(int x) { return 3-x; } }; int main() { C c return c-3; }
What is wrong with the combination of the two? Why can't an operator be applied after an implicit conversion? Are there any workarounds to solve this problem?
source share