Search code examples
c++c++14metaprogrammingboost-hana

hana::tuple to auto && ... args


Is there a way to use something like :

constexpr auto foo = hana::make_tuple(hana::type_c<Foo1>,hana::type_c<Foo2>);

with something like:

template < typename ... Ts >
struct Final {

  constexpr Final(Ts && ... args) {}
};

hana::unpack(foo, [] (auto && ... args) { return Final(args...); });

Because with that code, unpack can't deduce lambda/function type. Basically I want to create a type which takes a list of arguments but I have a tuple which contains the arguments.


Solution

  • The problem is in your lambda:

    [](auto && ... args){ return Final(args...); }
    //                          ~~~~~~~
    

    Final isn't a type, it's a class template. As such, you need to explicitly provide the types. Something like:

    [](auto&&... args){ return Final<decltype(args)...>(
        std::forward<decltype(args)>(args)...); }
    

    In C++17, with template deduction for class template parameters, the Ts&& does not function as a forwarding reference (see related answer), so the implicit deduction guide would not match your usage anyway as you are only providing lvalues and the guide requires revalues. But this would work:

    [](auto... args){ return Final(std::move(args)...); }