This is not going to end well. C++ itself provides no support for creating new operators, only providing custom per-type behavior for the existing ones. And the preprocessor does nothing per-type, nor has any support for infix notation.
I sure wouldn't try to use single-letter operators, since they'll get replaced everywhere in your program.
To make something like u cross v
work, you could
#define cross * cross_product_helper() *
And then use some techniques from expression trees -- the operator*(vec, cross_product_helper)
returns a helper type that holds the vector and knows it wants to do cross product, the second use of *
actually does the multiply.
On the other hand, would it be so bad to have the code written like:
u *cross* v
Where cross
is a pre-defined, properly scoped variable of type cross_product_helper
? (And of course you could have similar dot
and element
defined with unique types to make u *cross* v
, u *dot* v
, and u *element* v
(and u /element/ v
) all work and return different types.
Note that I've chosen operator*
as the foundation for *cross*
, as opposed to +cross+
or ^cross^
, because it has the precedence and other grammatical properties associated with products.