I have a task in my HW that require me to create a method that takes a tuple and a lambda function, applies this lambda function on each element of the tuple. at the end the method should return a new tuple containning the elements that returned true in lambda.
This is my tuple in main that i am filtering:
int main(){
auto tpl = make_tuple(
[](auto a, auto b) { return a + b; },
10,
25,
42.42,
"Hello",
string("there")
);
auto tpl_type_filtered = filterTuple(tpl, []<typename T>(const T&) { return is_integral_v<T>; } );
return 0;
}
This is my progress so far:
template <size_t Index = 0, typename... Types,typename... Types2, typename Func>
auto filterTupleHelper(const std::tuple<Types...>& tuple, const Func& func, const std::tuple<Types2...>& container) {
if constexpr (Index < sizeof...(Types)) {
if constexpr (func(std::get<Index>(tuple))) {
return filterTupleHelper<Index + 1>(tuple, func, std::tuple_cat(container, std::make_tuple(std::get<Index>(tuple))));
} else {
return filterTupleHelper<Index + 1>(tuple, func, container);
}
} else {
return container;
}
}
template <typename Tuple, typename Func>
auto filterTuple(const Tuple& tuple, const Func& func) {
return filterTupleHelper(tuple, func, std::make_tuple());
}
Also the teacher requested the following:
" this function must be protected by a "requires" clause so that it will be only available for tuple params alo "concept" core language keyword usage is FORBIDDEN.
You must use inline requires expressions and clauses.
You can write your own is_tuple/is_tuple_v structs if necessary"
but i dont know why or how to use requires in this case, and i dont get what concept would benifit me. I feel like i am completely lost or missing the point..
I would really apprecaite some help