Can iter_swap be specialized?

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).

+5
source share
3 answers

, iter_swap std::reverse, - : std::iter_swap. std, .

+2

iter_swap IndirectIterator, (, std::sort) ?

/. , iter_swap std.

, . , iter_swap std, std::sort . gcc std lib std::sort std::iter_swap.

, std::sort. IMO std::sort swap_iter.

i.e., , - ?

GCC ( ), std::iter_swap (?) , .

+1

You are allowed to rediscover the namespace stdand specialize templates internally stdif you specialize in custom types. In your case, you can actually specialize std::iter_swapfor your purposes, just make sure that you do this in the namespace stdand not in your own namespace (as in your example). It is not very elegant, but allowed.

0
source

All Articles