I'm trying to define a function using template template parameters (I just want to know how it works). I have the following:
template <typename T, template <typename> class Cont>
typename Cont<T>::iterator binary_search (typename Cont<T>::iterator first, typename Cont<T>::iterator last)
{
typename Cont<T>::iterator it;
// ...
return it;
}
Then in the main ()
function:
std::vector<int> data;
// ....
std::vector<int>::iterator it = binary_search (data.begin (),data.end ());
I get this error when trying to compile the code:
binary_search.cpp: In function ‘int main(int, char**)’:
binary_search.cpp:43:83: error: no matching function for call to ‘binary_search(std::vector<int>::iterator, std::vector<int>::iterator)’
I cannot find any appropriate response that helps me to sort out this error. Any help would be appreciated.
Thanks in advance
What you have is a non-deduced context, aswell as a template template parameter mismatch even if the context was deducible.
std::vector
takes a second template parameter, the allocator, that is defaulted tostd::allocator
.For the non-deduced context,
T
can never be deduced and will always have to be specified, thetypename
indicates this. See this question for the gory details.