Skip to content

Instantly share code, notes, and snippets.

@drvink
Created September 23, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drvink/cf89c578f3d5c84f004e to your computer and use it in GitHub Desktop.
Save drvink/cf89c578f3d5c84f004e to your computer and use it in GitHub Desktop.
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