0

Consider the following code simplified from some iterators I was writing:

#include <iterator>
#include <iostream>
#include <typeinfo>
#include <array>

struct NoTemplate:public std::iterator<std::forward_iterator_tag, 
                                       std::array<int, 5> >{
    static value_type v;
};

template<int n>
struct WithTemplate:public std::iterator<std::forward_iterator_tag, 
                                         std::array<int, n> >{
    //static value_type v;  //Doesn't work
    static typename std::iterator<std::forward_iterator_tag, std::array<int, n> >::value_type v;
};

int main(){
    std::cout << "NoT:" << typeid(NoTemplate::v).name() << "\nWT:" 
              << typeid(WithTemplate<5>::v).name() << "\n";
    return 0;
}

In NoTemplate, I can easily access std::iterator<std::forward_iterator_tag, array<int, 5> >::value_type as value_type. However, in WithTemplate I need to write everything out. Why?

If I replace the declaration of v in WithTemplate with the commented one, g++ 4.8.1 gives the error:

foo.cpp:14:12: error: ‘value_type’ does not name a type
     static value_type v;  //Doesn't work
            ^
foo.cpp:14:12: note: (perhaps ‘typename std::iterator<std::forward_iterator_tag, std::array<int, n> >::value_type’ was intended)
Eponymous
  • 6,143
  • 4
  • 43
  • 43
  • 4
    The answer to [this](https://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer) question is quite relevant. – David G Aug 29 '14 at 21:30
  • This SO answer explains it and quotes the relevant parts of the standard: http://stackoverflow.com/a/11425/434551. – R Sahu Aug 29 '14 at 21:49

0 Answers0