Created
July 24, 2014 05:27
-
-
Save thequux/7d81e5b3ad6dd5289391 to your computer and use it in GitHub Desktop.
Named operators! In C++!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class Self, typename L, typename R, typename Ret> | |
class Op { | |
public: | |
typedef L left_arg_type; | |
typedef R right_arg_type; | |
typedef Ret return_type; | |
//Ret operator() (L left, R right); | |
}; | |
template<class Op> | |
class LeftApp { | |
typename Op::left_arg_type &bound; | |
const Op &op; | |
public: | |
LeftApp<Op>(typename Op::left_arg_type &bound, const Op &op) : bound(bound), op(op) {} | |
inline typename Op::return_type operator>(typename Op::right_arg_type arg) { | |
return op(bound, arg); | |
} | |
}; | |
template<class Op> | |
inline LeftApp<Op> operator<(typename Op::left_arg_type arg, const Op &op) { | |
return LeftApp<Op>(arg, op); | |
} | |
class MulOp : public Op<MulOp, int, int, int> { | |
public: | |
int operator() (int a, int b) const { | |
return a * b; | |
} | |
} mul; | |
int main(int argc, char** argv) { | |
(void)argv; | |
return argc <mul> argc; | |
} |
I think you should create a templated free function so any two values that can be multiplied (or for which the expression a*b is valid!) will be accepted.
Oh, even better, reimplement using Boost.Proto!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WHY WOULD YOU DO THIS