vendredi 24 juin 2016

Perfect forward variadic arguments to lambda

Somehow I need to implement lazy evaluation with c++ variadic lambda. I am not quite sure whether the following code works correctly.

template <typename... ArgsT>
auto lazy_pack(ArgsT&& ... args) {

  auto T = [&](bool condition) {
    if(condition == false) return; 
    Foo v(std::forward<ArgsT>(args)...);
    v.do_work();
  };  

  return T;
}

The question is, how do I capture a given argument list and perfectly forward them to another templated object? The above example compiles but I am worried about if any dangling reference might happen. Another way is to capture arguments through copy and them and pass to the object:

template <typename... ArgsT>
auto lazy_pack(ArgsT&& ... args) {

  auto T = [=](bool condition) {
    if(condition == false) return; 
    Foo v(args...);
    v.do_work();
  };  

  return T;
}

Aucun commentaire:

Enregistrer un commentaire