Skip to content

Instantly share code, notes, and snippets.

@drvink drvink/compose.cpp Secret
Created Sep 23, 2015

Embed
What would you like to do?
function composition
#include <utility>
template <typename f1, typename f2>
struct compose_ {
f1 fun1;
f2 fun2;
template <typename... as>
auto operator()(as&&... args) ->
decltype(fun1(fun2(std::forward<as>(args)...)))
{
return fun1(fun2(std::forward<as>(args)...));
};
};
template <typename f1, typename f2>
compose_<f1, f2>
compose(f1&& fun1, f2&& fun2)
{
return { std::forward<f1>(fun1), std::forward<f2>(fun2) };
}
static int
add1(int x)
{
return x + 1;
}
static int
add2(int x)
{
return x + 2;
}
int
main()
{
auto f = compose(add1, add2);
return f(1);
}
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.