为什么在模板函数前面需要typename?

时间:2020-07-01 22:37:55

标签: c++ c++11

我使用的是Stroustrup C ++第4版第692页的示例。有人知道为什么typename必须先于Iter::value_type吗?特别是对于第二个mean函数,为什么此模板函数不需要typename

我已经按顺序阅读了Stroustrup的书,并且位于模板部分。我不记得阅读过有关其背后原因的文章。

感谢您的指导!

template<typename Iter>
typename Iter::value_type mean(Iter first, Iter last);

template<typename T>
T mean(T*,T*);

void f(vector<int>& v, int* p, int n)
{
    auto x = mean(v.begin(),v.end());
    auto y = mean(p,p+n);
}

0 个答案:

没有答案