Вопрос

I am trying to define the value_type of an iterator wrapper that dereferences the value_type of the wrapped iterator.

For iterators with a value_type that is an actual pointer this is trivial, but I need it to work for some "pointer-like" classes as well. In that case, the value_type should be the type returned by the wrapped iterator's value_type's operator->, sans pointer.

Here's a snippet of the current code, which works fine, but...

template <typename T>
struct remove_pointer {
    typedef typename remove_pointer<decltype(((T*)0)->operator->())>::type type;
};
template <typename T>
struct remove_pointer<T*> {
    typedef T type;
};

and

template <typename T_it>
class deref_iterator {
    typedef typename remove_pointer<
                         typename std::iterator_traits<T_it>::value_type
                     >::type value_type;
};

This works fine, but I really don't like the decltype(((T*)0)->operator->()) to get the return type of T's operator->.

Is there some nicer method to accomplish this?

Это было полезно?

Решение

I think your current code is very nearly fine. If I were writing this, I'd use declval rather than (T*)0, and I'd use the dereference operator rather than operator->:

template <typename T>
using pointed_to_type_t =
    typename std::remove_reference<decltype(*std::declval<T>())>::type;

template <typename T_it>
class deref_iterator {
    typedef pointed_to_type_t<
        typename std::iterator_traits<T_it>::value_type> value_type;
};
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top