Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created February 21, 2010 15:24
Show Gist options
  • Save eagletmt/310371 to your computer and use it in GitHub Desktop.
Save eagletmt/310371 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <tr1/functional>
template <typename F, typename G>
class on_class
: public std::binary_function<typename G::argument_type,
typename G::argument_type, typename F::result_type>
{
private:
F f;
G g;
public:
on_class(const F& f_, const G& g_)
: f(f_), g(g_)
{}
typename F::result_type
operator()(const typename G::argument_type& x, const typename G::argument_type& y) const
{
return f(g(x), g(y));
}
};
template <typename F, typename G>
on_class<F, G>
on(const F& f, const G& g)
{
return on_class<F, G>(f, g);
}
int main()
{
const std::tr1::function<int (int,int)> f = on(std::plus<int>(), std::negate<int>());
std::cout << f(3, -5) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment