How to get types from a package of variational parameters in a common lambda?

I am trying to write a function that returns a common lambda with variable arguments, where the lambda checks that one of the arguments is equal to a specific value. Here (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 am not sure what to add to the template argument std::tuple</* ??? */>. I tried to decltype(args), decltype(args...), auto, auto...and a few other things, but I keep getting compiler errors. Is it possible?

The nonequivalent equivalent would look something like this:

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 is not common - it does not work with any parameter package.

+4
2

, std:: tuple. decltype (args), decltype (args...), auto, auto... , .

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

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

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

Source: https://habr.com/ru/post/1665571/


All Articles