What is a Y combinator where I see some ppl using it, it looks something like this:
template<class F>
struct y_combinate_t {
F f;
template<class...Args>
decltype(auto) operator()(Args&&...args)const {
return f(*this, std::forward<Args>(args)...);
}
};
template<class F>
y_combinate_t<std::decay_t<F>> y_combinate( F&& f ) {
return {std::forward<F>(f)};
};
It's a workaround for recursive lambdas (see here). I personally use
std::function
which can be called within itself.Using
std::function
runs the risk of TLEing, for most competitive programming purposes it is way better to use self-referential lambdas or y_combinator (which is a wrapper for self-referential lambdas, though sometimes optimizations don't see through this wrapper).C++23 has a feature called
deducing this
which is a pretty good replacement for these gymnastics (in fact, it was meant to do other things but it having this property is a nice side effect).Interesting, although I have not experienced TLEs in the past.
aryanc403 wrote a blog about it , you can find it here.
https://www.ycombinator.com/