I found the URL below that says that
If an operator can be used as either a unary or a binary
operator (&, *, +, and -), you can overload each use separately.
I am working with g++ in Linux and I tried the following and it didn’t compile.
int operator+ (const int a,const int b){
std::cout << "MINE"<<std::endl;
return 0;
}
int main(){
char c='c';
std::cout << c+2 << std::endl;
}
The error says
error: ‘int operator+(int, int)’ must have an argument
of class or enumerated type
I was willing to play and see in action the Integer Promotion Rules.
Am I doing something wrong or that URL is valid only for MS or I misunderstood the promotion rule?
The error message indirectly tells you what you need to know — you are not permitted to overload operators (binary or unary) that act only on built-in types.
For a user-defined type
T, you can separately overload binary + (for example byT operator+(T lhs, T rhs)) and unary + (for example byT operator+(T t)). You could also defineoperator+(T lhs, int rhs), but you can’t overload addition of two integers.