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.
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);
};
}
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);
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With