4

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.

xinaiz
  • 7,744
  • 6
  • 34
  • 78

1 Answers1

4

typename Container<Tuple<TupleArgs...>>::iterator is a non-deduced context. The compiler can't work out what container an iterator came from, because there's not necessarily a one-to-one mapping between the two.

As you suggest, the best option is to just have the iterator types as template parameters so that they can be deduced correctly.

Community
  • 1
  • 1
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • 1
    Like one of my fellow programmers is used to say, 'compilers do not solve equations for types'. – SergeyA Jan 13 '17 at 15:52