So, I just came back to C++ after some break, I thought maybe I should write something for fun. Here I thought about function that searches through container of tuples and returns iterator if Nth element of tuple meets requirement (value or unary function).
So, here is what I have written by now:
template<std::size_t tupleIndex,
template<typename...> class Container,
template<typename...> class Tuple,
typename... TupleArgs>
auto find(typename Container<Tuple<TupleArgs...>>::iterator it1,
typename Container<Tuple<TupleArgs...>>::iterator it2,
decltype(std::get<tupleIndex>(std::declval<Tuple<TupleArgs...>>())) searchedValue) {
for(; it1 != it2; ++it1) {
if(std::get<tupleIndex>(*it1) == searchedValue)
break;
}
return it1;
}
But it fails on call:
using Tuple = std::tuple<int, float, bool>;
std::vector<Tuple> vec{std::make_tuple(1, 1.5, false),
std::make_tuple(2, 2.5, true),
std::make_tuple(3, 3.5, false)};
auto iter = find<0>(vec.begin(), vec.end(), 1); //error
The error:
error: no matching function for call to
'find(std::vector<std::tuple<int, float, bool> >::iterator, std::vector<std::tuple<int, float, bool> >::iterator, int)'
note: couldn't deduce template parameter 'template<class ...> class Container'
auto iter = find<0>(vec.begin(), vec.end(), 1);
^
Which I find strange because it fits perfectly in signature of candidate:
find(typename Container<Tuple<TupleArgs ...> >::iterator, typename Container<Tuple<TupleArgs ...> >::iterator, decltype (std::get<tupleIndex>(std::declval<Tuple<TupleArgs ...> >()))
Why Container
cannot be deduced in this case? I know it can be done by just templatizing iterators, but I want to be specific just in this case.