如何从通用 lambda 中的可变参数包中获取类型?

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

我正在尝试编写一个函数,它将 return 一个带有可变参数的 generic lambda,其中 lambda 检查其中一个参数是否等于特定值。这是(大致)我正在尝试做的事情:

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

我不确定要在 std::tuple</* ??? */> 模板参数中放入什么。我尝试了 decltype(args)decltype(args...)autoauto... 和其他一些方法,但我不断收到编译器错误。这可能吗?

非通用等效项类似于:

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

这工作正常,但 returned lambda 不是通用的 - 它不适用于任何任意参数包。

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.

试试

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

您可以通过简单地使用 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);
    };
}