#include <iostream>
#include <string>
#include <vector>
struct Type{
typedef std::string strType;
using intType = int;
};
struct Container{
using vecIterType=std::vector<int>::iterator;
};
// 当一个依赖于模板参数的名称代表的是某种类型的时候,就必须使用typename
template<typename T, typename U>
struct Person{
// T::strType str; //error , 编译器会理解为strType是T的static成员变量、枚举变量
// T::intType ivar;
typename T::strType str; // 前面加上typename让编译器理解strType是一个类型
typename T::intType intvar;
typename U::vecIterType it;
};
void Test(){
Person<Type, Container> p;
}
int main()
{
Test();
}
typname 使用
最新推荐文章于 2025-09-07 17:59:07 发布
本文详细探讨了C++中结构体内的类型别名以及模板参数依赖时的typename用法。通过示例展示了如何正确声明和使用typename来解决编译错误,同时解释了在模板类中typename关键字的重要性。
118

被折叠的 条评论
为什么被折叠?



