0

I am reading to an open source project. I am not able to understand what this snippet does ?

EXPORT Result LoaderParse(LoaderContext *Cxt, Context **Module, const char *Path) {
    return wrap([&]() {
        return fromloa(Cxt)->parse(std::filesystem::absolute(Path));
      }, [&](auto &&Res) {
          *Mod = toAST((*Res).release());
      }, Cxt, Module);
}


template <typename T, typename U, typename... CxtT>
inline Result wrap(T &&Proc, U &&Then, CxtT *...Cxts) noexcept {
    if (isC(Cxts...)) {
        if (auto Res = Proc()) {
            Then(Res);
            return 0;
        } else {
            return 1;
        }
    } else {
        return 2;
    }
}

Can anyone explain me what does [&] do in this case?

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49

1 Answers1

1

[&] alongside with [=] denotes a capture-default policy for the given lambda:

  • & (implicitly capture the used automatic variables by reference) and
  • = (implicitly capture the used automatic variables by copy).

For your particular case it means that two first closures (arguments) passed to the wrap function can access all variables in the given context (Cxt, Module and Path) by reference. There are other side effects a capture list may introduce, e.g. the lambda has deleted copy-assignment operator in C++20 and cannot be casted into a function pointer.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49