Skip to content

Instantly share code, notes, and snippets.

@thequux
Created July 24, 2014 05:27
Show Gist options
  • Save thequux/7d81e5b3ad6dd5289391 to your computer and use it in GitHub Desktop.
Save thequux/7d81e5b3ad6dd5289391 to your computer and use it in GitHub Desktop.
Named operators! In C++!
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;
}
@TerrorBite
Copy link

WHY WOULD YOU DO THIS

@jefftrull
Copy link

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.

@jefftrull
Copy link

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