Question

While I am aware this is a stupid idea, I wanted to see if I could use a single class for both container and non-container types. First, I copy-pasted code from this question.

Then I have two helper functions: one to determine the type of the member functions variables (whether or not T has the member value_type) and the other to determine the return value of 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() {
}

And my class looks like this:

template<typename T>
class MyIterator {

cur should be a pointer to T instead of a const_iterator if T does not have the value_type member. If this is the case, begin and end are unused.

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

That is the logic of my init function here.

    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;
    }

I've narrowed the problem down to this one line. If I remove init<T>(t) and copy-paste the contents of the first overload directly, I get proper results. Otherwise, I get incorrect results.

    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;
    }
};

For example, the incorrect output is:

0
0
3
4
5
h
i

It seems to be calling the correct function. What is the problem?

Edit

For some reason, changing the function signature to init(const U& t) { fixes the problem. Can anyone explain why?

The Code


Valgrind error:

==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 detects no errors when I don't call init<T>(t).


Was it helpful?

Solution

init accepting its parameter by value means that it is a copy of the original object. You are storing iterators from that copy, which is destroyed when init returns. Destroying a container invalidates its iterators, so dereferencing those iterators has undefined behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top