Skip to content

Instantly share code, notes, and snippets.

@tkoz0
Created April 11, 2024 00:36
Show Gist options
  • Save tkoz0/3da23af0e79292fdd14a5fd9ddc74b6c to your computer and use it in GitHub Desktop.
Save tkoz0/3da23af0e79292fdd14a5fd9ddc74b6c to your computer and use it in GitHub Desktop.
custom operator in c++/cpp not recommended
#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)
define operator(int, foo, int, int) {
return a + b;
}
#define foo <foo>
int main() {
return 4 foo 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment