0

Just for learning purposes and if I would really like to compare apples with pies. And ignoring for the moment rules and idioms

What type of parameter is allowed for overloaded operators, e.g. "operator !=" ?

Ok, it is expecting exactly one parameter, but what type?. Having read 'functions', 'declarations', 'types' and 'operator overloading', I still do not know it. Any restriction? Maybe the same as any other function? Can somebody confirm that?

Any help appreciated

Edit:

In my post I even linked the proposed dup. So, why mark it as dup. I read that thread before. My question is nowhere answered in the post. I am not sure how to rephrase the question. It is about the "C++ type" of the parameter in the overloaded operator function. A "C++ type" is something like described in types. For example, I think that the type must be complete. Any other restrictions or requirements?

A M
  • 14,694
  • 5
  • 19
  • 44
  • 1
    Have you tried anything? – Ulrich Eckhardt Dec 26 '18 at 12:06
  • @πάν i'm not sur where the dup answers the question. – YSC Dec 26 '18 at 12:15
  • I have even a link in my question to the proposed duplicate. I read this. It does not answer my question. Therefore I have asked. @gsamaras, πάντα ῥεῖ . Maybe you ust read the header of the question. – A M Dec 26 '18 at 14:03

2 Answers2

0

What type of parameter is allowed for overloaded operators, e.g. "operator !=" ?

There are very few restrictions, the most important being: you cannot overload operators for builtin types. So one of the two types for binary operators must be a user-defined type.

[over.oper]/6

An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators.

Community
  • 1
  • 1
YSC
  • 38,212
  • 9
  • 96
  • 149
  • Thank you for the answer. Good reference. That helped already, maybe not fully. Because it also works with pointers, for example with function pointers. Hm . . . – A M Dec 26 '18 at 14:11
  • @Armin Could you give me an example? doesn't work for me: http://coliru.stacked-crooked.com/a/e6dc4a74caa36b4e. – YSC Dec 26 '18 at 14:16
  • Something like (very sorry for bad format): "struct test{ bool operator != (bool(*)()) { return false; } }; test t; bool checkSomething(){ return false; } bool testop(){ return( t!= checkSomething); }". Compiled with gcc8.2 -Wall- Wextra -pedantic on RPI 3B+ – A M Dec 26 '18 at 20:30
  • @Armin, "at least one of the parameters...". So yes, the other one can be anything, including a pointer. Your operator takes as first hidden parameter a reference to a class type (`test&`). – YSC Dec 26 '18 at 22:18
0

According to cppreference, the function signature (for a free function) is:

bool operator !=(const T &a, const T2 &b);

Where:

T2 can be any type including T

Similar wording applies if operator!= is a class member function. Plus what YSC said.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Thank you. Although, one could guess that the operand must be const and a reference. But also a int*** works. If we see T as int ***, then it is maybe a "const int *** &". It accepts also a int&&. – A M Dec 26 '18 at 14:14