1

According to this question, typename should be added to tell compiler that iterator is a type (Is it correct?), e.g.

template <typename T>
void print(vector<T> &v) {
  for (typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
    cout<<*it<<endl;
}

Two questions:

  1. Under what circumstances should a typename be added? Can someone give me another example?

  2. Can auto in C++11 replace all these typename xxx cases?

Community
  • 1
  • 1
Deqing
  • 14,098
  • 15
  • 84
  • 131

1 Answers1

0
  1. When you are referring to a typename whose exact meaning depends on a template parameter.
  2. Not all, but many. auto has to be able to infer/deduce the type. In something like this:

    typename T:foo_t var;
    

for example, it can't.

David G
  • 94,763
  • 41
  • 167
  • 253
kec
  • 2,099
  • 11
  • 17