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)