Skip to content

Instantly share code, notes, and snippets.

@rileylev
Created December 22, 2018 08:46
Show Gist options
  • Save rileylev/048827c635636459c089b065df728609 to your computer and use it in GitHub Desktop.
Save rileylev/048827c635636459c089b065df728609 to your computer and use it in GitHub Desktop.
Y Combinator in C++2a
#include <utility>
template <class F>
struct Y{
F f;
Y(F _f) : f{_f} {}
template<class...arg_t>
auto operator()(arg_t&&...arg) {return f(*this,std::forward<arg_t>(arg)...);}
};
auto fact = Y{[](auto&& self, unsigned int n)->unsigned int {
if (n<=1) return 1;
return n*self(n-1)}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment