I want a mechanism to determine in compile time whether an iterator is reverse or not.
Iterator traits can only help with the category of an iterator type and what I need is something in the lines of:
template<typename IterType>
struct IsReverseIterator
{
enum { Yes = /* Implementation of the mechanism */ };
}
I have a solution, that has a small drawback though, the container type has to be provided as well :
typedef char TrueT;
typedef struct { TrueT _[2]; } FalseT;
template<typename Cont> TrueT IsReverseIterator(typename Cont::const_reverse_iterator);
template<typename Cont> FalseT IsReverseIterator(...);
It uses SFINAE obviously and can be utilized like so :
std::vector<int> v;
std::cout << (sizeof(IsReverseIterator<std::vector<int>>(v.begin())) == sizeof(TrueT)) << std::endl;
std::cout << (sizeof(IsReverseIterator<std::vector<int>>(v.rbegin())) == sizeof(TrueT)) << std::endl;
Any ideas?
EDIT
To explain what I'm searching for, take for example the following code
template<typename Cont, typename It>
bool points_to_last_element(Cont const &container, It iter)
{
return iter == container.rend();
// here I would like to dispatch to 2 implementations
// one for reverse iterators and one for forward where this would happen
// return iter == container.end();
}
It is a dummy example, please don't get caught on the fact that the code I have already handles it or that I could have two overloads, one taking Cont::reverse_iterator
and one taking Cont::iterator
. I don't/can't change that design, I'm just trying a more elegant way (if there's any) to handle it internally. Again I repeat that was a dummy example.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…