Search code examples
c++lambdac++14variadic-templatesgeneric-lambda

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.


Solution

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