声明template参数时, 前缀要害字class和typename可以交流;
利用要害字typename标识嵌套从属范例名称, 但不需在基类列表和成员初始化列表内利用.
从属名称(dependent names): 模板(template)内呈现的名称, 相依于某个模板(template)参数, 如T t;
嵌套从属名称(nested dependent names):从属名称在class内呈嵌套装, 如T::const_iterator ci;
非从属名称(non-dependent names): 不依赖任何template参数的名称, 如int value;
假如不特定指出typename, 嵌套从属名称, 有大概发生理会(parse)歧义.
任何时候在模板(template)中指涉一个嵌套从属范例名称, 需要在前一个位置, 添加要害字typename;
不然报错(GCC): error: need ‘typename’ before ‘T::xxx’ because ‘T’ is a dependent scope
代码:
/* * BInsertSort.cpp * * Created on: 2014.4.17. * Author: Spike */ #include <iostream> #include <string> #include <vector> using namespace std; template<typename T> void print2nd(const T& container) { typename T::const_iterator iter(container.begin()); //未加typename, 报错 ++iter; int value = *iter; std::cout << value; } int main () { vector<int> vi = {1,2,3,4,5}; print2nd(vi); return 0; }
输出:
2
破例:嵌套从属范例名称, 假如是基类列表(base class list)和成员初值列(member initialization list)中,不利用typename;
代码:
/* * BInsertSort.cpp * * Created on: 2014.4.17 * Author: Spike */ #include <iostream> #include <vector> using namespace std; struct Number { Number(int x) { std::cout << "Number = " << x << std::endl; } }; template<typename T> struct Base{ typedef Number Nested; }; template<typename T> class Derived: public Base<T>::Nested { //不消typename public: explicit Derived(int x) : Base<T>::Nested(x) { //不消typename typename Base<T>::Nested temp(7); //必需利用 } }; int main () { Derived<int> d(5); return 0; }
输出:
Number = 5 Number = 7
当利用特性类(traits class)时, 必需利用typename, 如
代码:
/* * BInsertSort.cpp * * Created on: 2014.4.17 * Author: Spike */ #include <array> #include <iostream> using namespace std; template<typename T> void workWithIter(T iter) { typedef typename std::iterator_traits<T>::value_type value_type; //利用typename value_type temp(*iter); std::cout << "temp = " << temp << std::endl; } int main () { std::array<int, 5> ai = {1,2,3,4,5}; std::array<int, 5>::iterator aiIter = ai.begin(); workWithIter(aiIter); return 0; }
输出:
temp = 1
作者:csdn博客 Spike_King