I want to write template function void foo() which takes 2 iterators or pointers.
I want to have a specialized version for random_access_iterator. I try to write like below:
// generic version
template<class Iter, typename std::iterator_traits<Iter>::iterator_category>
void foo(Iter first, Iter last);
// Specialized version for random_access_iterator
template<class RandomIter>
void foo<RandomIter, std::random_access_iterator_tag>(RandomIter first, RandomIter last)
{
}
It gives error: illegal use of explicit template arguments
Please let me know what is correct way of defining this. Thanks
Based on "tag dispatching" pattern, I've written a solution to my question:
// Using "= delete" on un-supported categories to get concise compile-time error information
template<typename Iter> void _foo(Iter first, Iter last, std::bidirectional_iterator_tag) = delete;
template<typename RandomIter>
void _foo(RandomIter first, RandomIter last, std::random_access_iterator_tag)
{ ... }
template<class Iter>
void foo(Iter first, Iter last)
{
typename std::iterator_traits<Iter>::iterator_category category;
_foo(first, last, category);
}
It works now. Please leave comment if it has bug or need improvement. Thanks.