Correct function search with replacement of function argument

So, I just returned to C ++ after some break, I thought that maybe I should write something for fun. Here I was thinking of a function that searches through a container of tuples and returns an iterator if the Nth element of the tuple matches the requirement (value or unary function).

So here is what I have written so far:

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 does not work 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

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

What I find strange, because it fits the candidate’s signature perfectly:

find(typename Container<Tuple<TupleArgs ...> >::iterator, typename Container<Tuple<TupleArgs ...> >::iterator, decltype (std::get<tupleIndex>(std::declval<Tuple<TupleArgs ...> >()))

Why Containercan not be deduced in this case? I know that this can be done simply by templatizing iterators, but I want to be specific in this case.

+4
1

typename Container<Tuple<TupleArgs...>>::iterator . , , "--".

, - , .

+4

Source: https://habr.com/ru/post/1666731/


All Articles