(23条消息) typname 使用_最后冰吻free的博客-CSDN博客

PHOTO EMBED

Thu Nov 24 2022 06:08:20 GMT+0000 (Coordinated Universal Time)

Saved by @leawoliu

#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();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
content_copyCOPY

https://blog.csdn.net/CodeHouse/article/details/126433212