An operator can be declared only for the syntax defined for it in the C++ grammar. For example, one can’t define a unary % or a ternary +.
Consider the output operator for a class A. It has the following signature as a non-member:
ostream& operator<<(ostream&, A&);
This signature can't be changed. operator<< is a binary operator. Ie, it can take only 2 arguments.
The same is the case with the corresponding >> operator.
In certain situations, this can be restrictive, since it doesn't give the user of this operator, the required flexibility.
For example, consider a class Money used to store a monetary amount and its output operator:
ostream& operator<<(ostream&, Money&);
Since a monetary value is involved, we need to display the currency symbol also, which could be either the local or international symbol. If the user should be able to specify this, we would need the above operator to have another parameter, say bool intl. The operator's signature would then be:
ostream& operator<<(ostream&, bool intl, Money&);
Of course, this isn't possible, since the signature is fixed.
How can we proceed in such a situation?
Thanks.