c++ - I inherit from std::iterator, but compiler does not recognise 'pointer' or 'reference' -


i in process of making custom iterator, puzzled on makes compiler not recognise pointer or reference typedefs in case of templated iterator implementation.

the following code compiles fine:

#include <iterator>  struct : std::iterator<std::random_access_iterator_tag, int> {     int *v;     pointer operator-> () { return v; }     reference operator* () { return *v; }  }; 

but once templatize custom iterator errors:

#include <iterator>  template <class t> struct it2 : std::iterator<std::random_access_iterator_tag, t> {     t *v;     pointer operator-> () { return v; }     reference operator* () { return *v; }  }; 

whole code here

error: 'pointer' not name type note: (perhaps 'typename std::iterator<std::random_access_iterator_tag, t>::pointer' intended) error: 'reference' not name type ... 

1> why can't compiler see pointer , reference definitions contained in std::iterator ?

according note, seems shouldn't have bothered using std::iterator struct, instead should have manually copied typedefs. seems error prone in case iterator or iterator_traits typedef in future.

2. how think should deal definition of these traits (pointer, reference etc.) ?

1> why can't compiler see pointer , reference definitions contained in std::iterator ?

because compiler cannot rule out attempting provide specialisation of std::iterator pointer , reference aren't type definitions, dependent names can never interpreted typedef @ template definition time.

2. how think should deal definition of these traits (pointer, reference etc.) ?

while explicitly qualifying them typename every time works, can instead declare them once such, , rely on 1 declaration in rest of it2 definition:

#include <iterator>  template <class t> struct it2 : std::iterator<std::random_access_iterator_tag, t> {     using base = std::iterator<std::random_access_iterator_tag, t>;     using typename base::pointer;     using typename base::reference;     t *v;     pointer operator-> () { return v; }     reference operator* () { return *v; } }; 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -