When should I use typename in a template function?

Saturday, May 3, 2014

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?









http://ift.tt/1fGh3lP