Fefux Fefux - 11 months ago 48
C++ Question

Some explaination about C++ code (lambda wrapper and variadic template)

Can someone explain to me what does this piece of code step by step? I found it in this topic : Segmentation fault on gcc caused by lambda wrapper over variadic template function call and I don't understand nothing :

template <typename TNode, typename... TNodes>
auto execute(TNode& n, TNodes&... ns)
[&](){ n.execute(ns...); }();

Especially the part :
[&](){ n.execute(ns...); }();

Is there any connection with lambda calculus and programmation language like caml or ocaml?

Thank you in advance

Answer Source

This part [&](){ n.execute(ns...); }(); creates a new lambda and execute it directly. It is equal to:

auto temp= [&](){ n.execute(ns...); };

This part n.execute(ns...); is calling a member function called TNode::execute which accepts many parameters (variadic template argument) of the types TNodes...