Is it possible to make a custom operator so you can do things like this?
if ("Hello, world!" contains "Hello") ...
Note: this is a separate question from "Is it a good idea to..." ;)
Is it possible to make a custom operator so you can do things like this?
if ("Hello, world!" contains "Hello") ...
Note: this is a separate question from "Is it a good idea to..." ;)
I've created the following two macros:
#define define const struct
#define operator(ReturnType, OperatorName, FirstOperandType, SecondOperandType) OperatorName ## _ {} OperatorName; template <typename T> struct OperatorName ## Proxy{public:OperatorName ## Proxy(const T& t) : t_(t){}const T& t_;static ReturnType _ ## OperatorName ## _(const FirstOperandType a, const SecondOperandType b);};template <typename T> OperatorName ## Proxy<T> operator<(const T& lhs, const OperatorName ## _& rhs){return OperatorName ## Proxy<T>(lhs);}ReturnType operator>(const OperatorName ## Proxy<FirstOperandType>& lhs, const SecondOperandType& rhs){return OperatorName ## Proxy<FirstOperandType>::_ ## OperatorName ## _(lhs.t_, rhs);}template <typename T> inline ReturnType OperatorName ## Proxy<T>::_ ## OperatorName ## _(const FirstOperandType a, const SecondOperandType b)
Then, you'd have just to define your custom operator as in the following example:
define operator(bool, myOr, bool, bool) { // Arguments are the return type, the name of the operator, the left operand type and the right operand type, respectively
return a || b;
}
#define myOr <myOr> // Finally, you have to define a macro to avoid to put the < and > operator at the start and end of the operator name
Once a time you've set your operator up, you can use it as a predefined operator:
bool a = true myOr false;
// a == true
While this has been an interesting exercise, it merely demonstrates how bad is to have a macro–enabled precompiler. Adding custom operators like this can easily lead to a sort of metalanguage. Although we know how badly is C++ designed (most of all considering that it was first conceived as a set of extensions for C), we shouldn't be changing it. If you can't use standard C++, which is the only way to keep the code understandable by other people, you should just switch to another language that makes what you wish to do the way you'd like. There are thousands languages — no need to mess around with C++ to make it different.
SHORTLY: You just shouldn't be using this code. You should refrain from using macros unless when only used the same way as inline methods.
As others have pointed out you sadly can not write custom operators but with macros you can get similar behaviour. It is actually really easy with c style casting see below.
class To_Range{
public:
size_t start;
size_t end;
To_Range(size_t _start,size_t _end) :
start(_start), end(_end) {}
};
class Slicing_To_End{
public:
int end;
Slicing_To_End(const int& init) : end(init) {}
};
To_Range operator == (const int& start,const Slicing_To_End& end) {
return To_Range(start,end.end);
}
#define to == (Slicing_To_End)
Here 4 to 5 will give back an object of type To_Range. (Slicing_To_End) casts 5 to Slicing_To_End. Now the compiler wants to find an == operator that fits. The only one is our custom operator that takes as input an integer on the first position and in the second Slicing_To_End and returns our type To_Range. You also could return of course other types like int,float.