Say I need to use s
:
typedef struct tagSOMESTRUCT // Defined by someone else; C-compatible
{
int count;
int elements[256];
} SOMESTRUCT;
SOMESTRUCT s;
and say I have a function like:
template<typename RevFwdIt>
std::pair<RevFwdIt, RevFwdIt> some_slice_rev(RevFwdIt rbegin, RevFwdIt rend)
{
RevFwdIt it = basename_rev(rbegin, rend);
RevFwdIt e = std::find(rbegin, it, 5);
return std::make_pair(e == it ? rbegin : e, it);
}
In order to use this function, I need to say
some_slice_rev(&s.elements[s.count - 1], &s.elements[-1]);
which (IMHO) is ugly and error-prone due to the off-by-one errors.
On the one hand, I cannot simply change some_slice_rev
to some_slice
to use the (much better)
some_slice(&s.elements[0], &s.elements[s.count]);
because then std::find
would search from the beginning instead of the end.
On the other hand, the code itself already looks broken to me, because I can't see how std::find
would handle "reverse iterators" that are raw pointers.
What is the best way to fix the code in situations like this? Is there any way to work with reverse-iterators that are raw pointers? Or is there a standard refactoring mechanism for fixing this, other than changing SOMESTRUCT
?