Function template accepting nothing less than a bidirectional iterator or a pointer
Posted
by
san
on Stack Overflow
See other posts from Stack Overflow
or by san
Published on 2012-11-12T19:23:44Z
Indexed on
2012/11/12
23:01 UTC
Read the original article
Hit count: 207
I need a function template that accepts two iterators that could be pointers. If the two arguments are random_access iterators I want the return type to be an object of
std::iterator<random_access_iterator_tag, ...>
type
else a
std::iterator<bidirectional_iterator_tag, ...>
type.
I also want the code to refuse compilation if the arguments are neither a bidirectional iterator, nor a pointer. I cannot have dependency on third party libraries e.g. Boost
Could you help me with the signature of this function so that it accepts bidirectional iterators as well as pointers, but not say input_iterator, output_iterator, forward_iterators.
One partial solution I can think of is the following
template<class T>
T foo( T iter1, T iter2) {
const T tmp1 = reverse_iterator<T>(iter1);
const T tmp2 = reverse_iterator<T>(iter2);
// do something
}
The idea is that if it is not bidirectional the compiler will not let me construct a reverse_iterator from it.
© Stack Overflow or respective owner