Say that I want to make up a special operator !+ in C++ between two objects. I would like to use !+, on example, because I think it is much more meaningful than any other operator.
One basic thing I could do is to find a free, unused operator and make the replacement work with a #define:
#define !+ %
class myclass
{
public:
int operator %(myclass &c)
{
return 3;
}
}
So that if I later write something like
a!+b
with a and b instances of myclass, it would work.
Now, is there any way to define it instead that with an operator, with some function? Something like:
#define a!+b -> a.operatorexclamativeplus(b)
It would make the translation much less dirty, and would allow for a potentially unlimited number of these new "fake operators"!
It is not possible to define your own unique operators in C++.
I also second that it might be possible in other languages to do so.
No,
!+
doesn't form a valid (normal) identifier for a preprocessor macro. The characters are reserved for language intrinsic operators.The set of characters you can use for macro definitions is
[_A-Za-z][_A-Za-z0-9]*
regex syntax1.As from the c++ standards definitionsdraft section
UPDATE:
I've been experimenting with this a bit, since this actually is an interesting question, and if not
#define
'd macros, we can use overloading intrinsic operator functions for classes in c++.I have found there actually are possible (but may be weird and unexpectedly behaving) options, to overload combinations of intrinsic operators by chaining them and keep state. The closest thing you can get for your newly introduced operator could be:
Output
See live sample.
Though, because of operator precedence rules, this would work only for unary operators (
!
has higher precedence than binary+
), the form you want to have doesn't seem to be actually achievable:See this miserably fail.
CONCLUSION:
______________________________________________________________________________________
1) Regarding leading underscores (
_
,__
) for identifiers please read the standards sections mentioned in this answer, these are syntactically valid though.Others explained about the impossibility of this. However, you also asked about making this a function, and yes, that's' the way I would go first. You simply put whatever operation you want into a (free/unbound) function, giving it a suitable name, which will not cause any confusion to the reader.
Now, if you decided that your special case is special enough to break the rules, then you could do the following:
Here,
strangeop
is actually an object and there are overloaded/
operators for objects of this type and integers. The first such operator evaluates to a proxy object which keeps a reference to the integer operand but also defines overloaded/
operators in which it receives the other operand and then returns the actual result of the strange operation. Note here that you could use any binary operator here, it's more a matter of personal choice.Notes:
#define STRANGEOP /strangeop/
.strange_op(23, 43)
and consider it for its better readability.Technically, C++ does not let you define new operators. However, you can get any behavior you want out of existing operators, including the appearance of having a new operator.
(Not saying it's a good idea, just answering the question "Is it possible?")
You can not ever write
a !+ b
, becuase!
is not a binary operator.But you can write
a +! b
because+
is binary and!
is unary.And you can redefine those operators so that
a +! b
returns what you want.The trick is that you define the unary operator to return a helper object, and then define the binary operator to act upon that helper object.
As an example, here is code that will create the appearance of a +! operator. This new +! operator returns ten times the left hand side plus the right hand side. So... 1 +! 2 would yield 12.
You cannot define new operators in C/C++. The standard disallows it, and the metaprogramming capabilities of macros and templates are not powerful enough to get around it.
But did that stop Brad Cox (Objecte-C) and Bjorne Stroustrup (C++/cfront)? Of course not! Bertrand Meyer did it too (Eiffel) and so did Kernighan and Plauger (RATFOR).
They way you add new language features such as new operators to C/C++ is by creating your own pre-processor. You can use a macro processing language or write your own compiler so that the input:
Is translated into:
And the C++ compiler is perfectly happy.
You never know, your new language might even catch on. Sometimes they do.