no matching function error using template template

2020-04-20 11:46发布

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

1条回答
家丑人穷心不美
2楼-- · 2020-04-20 12:36

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 to std::allocator.

For the non-deduced context, T can never be deduced and will always have to be specified, the typename indicates this. See this question for the gory details.

查看更多
登录 后发表回答