STL模版在vs2019和gcc中的特殊问题
任何时候在模板(template)中使用一个嵌套从属类型名称, 需要在前一个位置, 添加关键字typename;
比如下面程序中使用迭代器类型时,就要使用typename.虽然在vs2010 和vs2015中没有错误,但在VC++2019和gcc编译器中,都会报错。
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <Windows.h>using namespace std;string line(50, '-');template <typename T>
void printInf(const list<T>& object)throw() {/* vs2019 gcc 中定义const_iterator前面必须加 typename* typename list<T>::const_iterator */typename list<T>::const_iterator citor = object.begin();for (; citor != object.end(); citor++) {cout << *citor <<endl;}cout << endl;cout << "size: " << object.size() << endl;cout << line << endl;return;
}class Student {
public:Student() {cout << "默认构造函数调用!" << endl;this->m_name = "***";this->m_age = 0;}Student(string name,int age) {cout << "带参构造函数调用!" << endl;this->m_name = name;this->m_age = age;}Student(const Student& stu) {cout << "拷贝构造函数调用!" << endl;this->m_name = stu.m_name;this->m_age = stu.m_age;}~Student() {cout << "析构函数调用!" << endl;}friend ostream& operator<<(ostream& out, const Student& stu);
private:string m_name;int m_age;
};ostream& operator<<(ostream& out, const Student& stu) {out << stu.m_name << " 年龄: " << stu.m_age ;return out;
}int main() {Student s1("对小帅", 19);Student s2("旺达", 21);Student s3("李小", 29);Student s4("谨记帅", 24);Student s5("常常", 22);Student s6("呼呼", 23);cout << line << endl;list<Student> stuList;stuList.push_back(s1);stuList.push_back(s2);stuList.push_back(s3);stuList.push_back(s4);stuList.push_back(s5);stuList.push_back(s6);cout << line << endl;printInf(stuList);system("pause");return 0;
}