variadic: add filter call

This commit is contained in:
Danny Robson 2018-03-23 16:40:30 +11:00
parent cceadcba40
commit 86b1fa38c7

View File

@ -20,6 +20,7 @@
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <tuple>
namespace util::variadic { namespace util::variadic {
@ -53,7 +54,32 @@ namespace util::variadic {
else else
return get<IndexV-1> (std::forward<TailT> (tail)...); return get<IndexV-1> (std::forward<TailT> (tail)...);
} }
}
///////////////////////////////////////////////////////////////////////////
/// returns a tuple of all arguments that satisfy the trait QueryT
template <template <typename> class QueryT>
auto filter () { return std::tuple {}; }
/// returns a tuple of all arguments that satisfy the trait QueryT
template <
template <typename> class QueryT,
typename HeadT,
typename ...ArgsT
>
auto
filter (HeadT &&head, ArgsT &&...args)
{
if constexpr (QueryT<HeadT>::value)
return std::tuple_cat (
std::tuple (std::forward<HeadT> (head)),
filter<QueryT> (std::forward<ArgsT> (args)...)
);
else
return filter<QueryT> (std::forward<ArgsT> (args)...);
}
};
#endif #endif