简体   繁体   中英

assignment operator overload on a template class with template parameters

I am designing my own iterator class so that I can create custom iterators for my containers. I figured this was the simplest approach, rather than inheriting from bidirectional_iterator or iterator_traits. The problem is that the containers are also templated. When writing my copy constructor and assignment operators for this class, the compiler does not like the return type nor the parameter (which is an

iterator<someContainer<someClasstype>>.

Is this a problem that can be fixed? Or is this a limitation of how deep templates can go?

Here is the class:

template <template<class Data> class Cont, class T>
class iterator
{
    typedef typename Cont<T>    container_type;
    typedef T*      ptr_type;
    typedef T       value_type;
private:
    ptr_type _ptr;
    size_t _alloc;  // offset to apply when jumping contiguos addresses
public:
    // ctors

    // Default
    explicit iterator()
    {
        _ptr = 0;
        _alloc = sizeof(value_type);
    }

    // reference
    explicit iterator(const value_type& address): _ptr(address)
    {
            _alloc = sizeof(value_type);
    }

    // pointer
    explicit iterator(const ptr_type ptr): _ptr(ptr)
    {
        _alloc = sizeof(value_type);
    }

    // copy
    iterator(const iterator<Cont<T>, T>& right)
    {
        _ptr = right._ptr;
        _alloc = right._alloc;
    }


    // operators

    // assignment
    iterator<Cont<T>, T>& operator=(const value_type& address)
    {
        return *this(address);
    }

    iterator<Cont<T>, T>& operator=(const ptr_type ptr)
    {
        return *this(ptr);
    }

    iterator<Cont<T>, T>& operator=(const iterator<container_type, T>& right)
    {
        return *this(right);
    }

    // equality
    bool operator==(const iterator<container_type, T>& right)
    {
        return (_ptr == right._ptr && _alloc == right._alloc);
    }

    // dereference
    T& operator*(const iterator<container_type, T>& it)
    {
        return *_ptr;
    }

    T* operator()   // get value operator? (ie list<int>::iterator returns the memory address, even though its a class
    {
        return _ptr;
    }

};

I have so far tried these combinations:

iterator<Cont<T>>
iterator<Cont<T>, T>
iterator<container_type> // typedef of Cont<T>
iterator<container_type, T> 

but none of them are accepted. Compiler errors are:

Error   1   error C3200: 'Cont<T>' : invalid template argument for template parameter 'Cont', expected a class template c:\users\sapphire\documents\visual studio 2012\projects\hybridlist\hybridlist\iterator.h    43

Error   2   error C2976: 'iterator' : too few template arguments    c:\users\sapphire\documents\visual studio 2012\projects\hybridlist\hybridlist\iterator.h    53

iterator 's first template parameter is a template template parameter. That is, it should take a template as its argument. You can't give Cont<T> because that is a specific instantiation of a template. Try:

iterator<Cont, T>

It's worth noting that Data in template<class Data> class Cont is redundant. Just template<class> class Cont will do fine.

Consider inheriting from std::iterator - this is what it's designed for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM