lundi 29 juillet 2019

Cannot dereference generic iterator type

I was just trying to build something that involved a generic iterator type, specifically something that chains iterators, but I cannot dereference the iterator. MVE:

#include <iterator>

#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1};
    std::iterator<std::random_access_iterator_tag, int> my_iterator = a.begin();
    int my_int = *my_iterator;
    return 0;
}

Error:

iterator.cxx:6:57: error: no viable conversion from 'std::__1::vector<int, std::__1::allocator<int> >::iterator'
      (aka '__wrap_iter<int *>') to 'std::iterator<std::random_access_iterator_tag, int>'
    std::iterator<std::random_access_iterator_tag, int> my_iterator = a.begin();
                                                        ^             ~~~~~~~~~
/Applications/Xcode-9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:531:29: note: 
      candidate constructor (the implicit copy constructor) not viable: no known conversion from
      'std::__1::vector<int, std::__1::allocator<int> >::iterator' (aka '__wrap_iter<int *>') to 'const
      std::__1::iterator<std::__1::random_access_iterator_tag, int, long, int *, int &> &' for 1st argument
struct _LIBCPP_TEMPLATE_VIS iterator
                            ^
/Applications/Xcode-9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:531:29: note: 
      candidate constructor (the implicit move constructor) not viable: no known conversion from
      'std::__1::vector<int, std::__1::allocator<int> >::iterator' (aka '__wrap_iter<int *>') to
      'std::__1::iterator<std::__1::random_access_iterator_tag, int, long, int *, int &> &&' for 1st argument
1 error generated.

Is there some other generic iterator class I'm unaware of that has this functionality?

Aucun commentaire:

Enregistrer un commentaire