I want to chain/compose multiple lambdas to a new lambda. When calling the new lambda with parameters it should call the original lambdas with these parameters. This lambda chaining does work well when the parameters are just copied but when I want to modify them perfect forwarding does not work:
#include <boost/hana.hpp>
#include <cassert>
constexpr auto chain_lambdas = [](auto... lambdas) {
constexpr auto lambdasTpl = boost::hana::make_basic_tuple(lambdas...);
return [lambdas(lambdasTpl)](auto&&... args) {
auto argsTpl = boost::hana::make_basic_tuple(std::forward<decltype(args)>(args)...);
boost::hana::for_each(lambdas, [args(argsTpl)](auto lambda) mutable {
boost::hana::unpack(std::move(args), [lambda(lambda)](auto&&... args) {
lambda(std::forward<decltype(args)>(args)...);
});
});
};
};
auto main() -> int
{
auto lambda1 = [](bool& called){
called = true;
};
auto lambda2 = [](bool& called){
called = true;
};
bool called = false;
chain_lambdas(lambda1, lambda2)(called);
assert(called == true);
return 0;
}
Code at the compiler explorer. Does perfect forwarding even work for this use case?