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

1.4k views Asked by At

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.

2

There are 2 answers

0
max66 On BEST ANSWER

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);
    };
}
0
Philipp Lenk On

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);
    };
}