Skip to content

Instantly share code, notes, and snippets.

@ahoka ahoka/infix2.cc
Created Feb 23, 2016

Embed
What would you like to do?
#include <functional>
#include <iostream>
template <class A, class B, class C>
class Infix
{
private:
std::function<C(A, B)> funM;
public:
Infix(std::function<C(A, B)> fun)
: funM(fun)
{
}
class Partial
{
private:
std::function<C(B)> funM;
public:
Partial(std::function<C(A, B)> fun, A a)
{
funM = [a, fun] (B b) -> C {
return fun(a, b);
};
}
friend C
operator|(Partial p, B b)
{
return p.funM(b);
}
};
friend Partial
operator|(A a, Infix i)
{
return Partial(i.funM, a);
}
};
int
main()
{
Infix<int, int, int> add([](int a, int b) { return a + b; });
int val = 1 |add| 2;
std::cout << val << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.