Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get types from variadic parameter pack in generic lambda?

I'm trying to write a function that will return a generic lambda with variadic arguments, where the lambda checks that one of the arguments is equal to a specific value. Here's (roughly) what I'm trying to do:

template <int Index, typename TValue>
inline auto arg_eq(const TValue& value) 
{
    return [value] (auto... args) -> bool {
        return (std::get<Index>(std::tuple</* ??? */>(args...)) == value);
    };
}

I'm not sure what to put in the std::tuple</* ??? */> template argument. I've tried decltype(args), decltype(args...), auto, auto..., and a few other things, but I keep getting compiler errors. Is this even possible?

The non-generic equivalent would be something like:

template <int Index, typename TValue, typename... TArgs>
inline auto arg_eq(const TValue& value)
{
    return [value] (TArgs... args) -> bool {
        return (std::get<Index>(std::tuple<TArgs...>(args...)) == value);
    };
}

This works fine, but the returned lambda isn't generic - it doesn't work with any arbitrary parameter pack.

like image 462
Chris Vig Avatar asked Dec 08 '22 19:12

Chris Vig


2 Answers

You could avoid mentioning the types by simply using std::make_tuple:

template <int Index, typename TValue>
auto arg_eq(const TValue& value) 
{
    return [value] (auto... args) -> bool {
        return (std::get<Index>(std::make_tuple(args...)) == value);
    };
}
like image 120
Philipp Lenk Avatar answered Dec 10 '22 10:12

Philipp Lenk


I'm not sure what to put in the std::tuple template argument. I've tried decltype(args), decltype(args...), auto, auto..., and a few other things, but I keep getting compiler errors.

Try with

std::tuple<decltype(args)...>

The full function

template <int Index, typename TValue>
inline auto arg_eq(const TValue& value) 
{
    return [value] (auto... args) -> bool {
        return (std::get<Index>(std::tuple<decltype(args)...>(args...)) 
                 == value);
    };
}
like image 21
max66 Avatar answered Dec 10 '22 08:12

max66