0

考虑以下从我正在编写的一些迭代器中简化的代码:

#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;
}

NoTemplate中,我可以很容易地访问std::iterator<std::forward_iterator_tag, array<int, 5> >::value_typeas value_type。但是,WithTemplate我需要把所有东西都写出来。为什么?

如果我用注释的声明替换vin的声明WithTemplate,g++ 4.8.1 会给出错误:

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)
4

0 回答 0