简体   繁体   English

赋值运算符使用模板参数重载模板类

[英]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. 我认为这是最简单的方法,而不是继承自bidirectional_iterator或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. iterator的第一个模板参数是模板模板参数。 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. 您不能授予Cont<T>因为这是模板的特定实例。 Try: 尝试:

iterator<Cont, T>

It's worth noting that Data in template<class Data> class Cont is redundant. 这是值得注意的, Datatemplate<class Data> class Cont是多余的。 Just template<class> class Cont will do fine. 只需要template<class> class Cont就可以了。

Consider inheriting from std::iterator - this is what it's designed for. 考虑继承自std::iterator - 这就是它的设计目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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