Блог пользователя akasakaR

Автор akasakaR, история, 5 недель назад, По-английски

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)};
};
  • Проголосовать: нравится
  • +18
  • Проголосовать: не нравится

»
5 недель назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

It's a workaround for recursive lambdas (see here). I personally use std::function which can be called within itself.

  • »
    »
    5 недель назад, # ^ |
    Rev. 3   Проголосовать: нравится +16 Проголосовать: не нравится

    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).

»
5 недель назад, # |
  Проголосовать: нравится +26 Проголосовать: не нравится

aryanc403 wrote a blog about it , you can find it here.

»
5 недель назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится