If I have a container std::vector<T*> items, I can create IndirectIteratorone that wraps std::vector<T*>::iteratorand allows iteration over T, rather than T*.
Can I specialize iter_swapfor IndirectIteratorstandard algorithms (e.g. std::sort) to replace elements with a pointer?
ie, if I write the following, will this have any effect on standard algorithms?
namespace some_namespace
{
template <typename IterT>
class IndirectIterator
{
IterT m_base;
public:
typedef IterT base_iterator;
typedef reference;
reference operator*() const { **m_base; }
const base_iterator& base() const { return m_base; }
base_iterator& base() { return m_base; }
};
template <typename T>
void iter_swap(IndirectIterator<T>& a, IndirectIterator<T>& b)
{
using std::iter_swap;
iter_swap(a.base(), b.base());
}
}
The advantage of this specialization is that it replaces pointers rather than full T instances, so itβs faster (potentially).
source
share