0

虽然我知道这是一个愚蠢的想法,但我想看看是否可以对容器和非容器类型使用单个类。首先,我从这个问题中复制粘贴代码。

然后我有两个辅助函数:一个确定成员函数变量的类型(是否T有成员value_type),另一个确定operator *.

template <typename T>
typename std::enable_if<HasValueType<T>::value, typename T::value_type>::type
proxy_func_op() {

}

template <typename T>
typename std::enable_if<!HasValueType<T>::value, T>::type
proxy_func_op() {

}

template <typename T>
typename std::enable_if<HasValueType<T>::value, typename T::const_iterator>::type
proxy_func_mem() {
}

template <typename T>
typename std::enable_if<!HasValueType<T>::value, T*>::type
proxy_func_mem() {
}

我的课看起来像这样:

template<typename T>
class MyIterator {

cur应该是指向T而不是const_iteratorifT没有value_type成员的指针。如果是这种情况,则 begin 和 end 未使用。

    decltype(proxy_func_mem<T>()) begin;
    decltype(proxy_func_mem<T>()) end;
    decltype(proxy_func_mem<T>()) cur;
public:

这就是我的 init 函数的逻辑。

    template <typename U = T>
    typename std::enable_if<HasValueType<U>::value, void>::type 
    init(U t) {
        static_assert(std::is_same<typename T::const_iterator, 
            decltype(proxy_func_mem<U>())>::value, 
            "Make sure correct function is called.");
        begin = t.begin();
        end = t.end();
        cur = begin;
    }

    template <typename U = T>
    typename std::enable_if<!HasValueType<U>::value, void>::type 
    init(U t) {
        static_assert(!std::is_same<typename T::const_iterator, 
        decltype(proxy_func_mem<U>())>::value, 
        "Make sure correct function is called.");
        cur = &t;
    }

我已将问题缩小到这一行。如果我直接删除init<T>(t)并复制粘贴第一个重载的内容,我会得到正确的结果。否则,我会得到不正确的结果。

    explicit MyIterator(const T& t) {
        init<T>(t);
    }

    MyIterator& operator++() {
        static_assert(HasValueType<T>::value, "You cannot use this operator for non-containers.");
        if (cur + 1 != end)
            cur++;
        return *this;
    }

    decltype(proxy_func_op<T>()) operator *() {
        return *cur;
    }
};

例如,不正确的输出是:

0
0
3
4
5
h
i

它似乎正在调用正确的函数。问题是什么?

编辑

出于某种原因,更改函数签名以init(const U& t) {解决问题。谁能解释为什么?

编码


Valgrind 错误:

==4117== Invalid read of size 4
==4117==    at 0x401270: MyIterator<std::vector<int, std::allocator<int> > >::operator*() (main.cpp:78)
==4117==    by 0x400E8A: main (main.cpp:87)
==4117==  Address 0x514d0a0 is 0 bytes inside a block of size 20 free'd
==4117==    at 0x4A05FD6: operator delete(void*) (vg_replace_malloc.c:480)
==4117==    by 0x401CC5: __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) (new_allocator.h:110)
==4117==    by 0x401999: std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) (stl_vector.h:174)
==4117==    by 0x4014A4: std::_Vector_base<int, std::allocator<int> >::~_Vector_base() (stl_vector.h:160)
==4117==    by 0x4011A0: std::vector<int, std::allocator<int> >::~vector() (stl_vector.h:416)
==4117==    by 0x401209: MyIterator<std::vector<int, std::allocator<int> > >::MyIterator(std::vector<int, std::allocator<int> > const&) (main.cpp:67)
==4117==    by 0x400E75: main (main.cpp:85)

当我不调用时,Valgrind 没有检测到错误init<T>(t)


4

1 回答 1

1

init通过值接受其参数意味着它是原始对象的副本。您正在存储该副本中的迭代器,该副本在init返回时被销毁。销毁容器会使其迭代器无效,因此取消引用这些迭代器具有未定义的行为。

于 2013-12-04T15:02:27.777 回答