C++随笔之typename关键字学习记录

C++之typename关键字学习

参考知无涯之C++ typename的起源与用法

typename其实就是使用模版类时,避免了实例化才能确定typename后面的对象是一个类型。如果不使用typename,那么只能在实例化时,才能知道对象是一个类型还是一个变量。
类型:
在这里插入图片描述
变量:
在这里插入图片描述
typename关键字修饰
在这里插入图片描述
typename使用规则
在这里插入图片描述

#include <iostream>
#include "test_line.h"
#include <vector>
#include "algorithm"
using namespace std;
template <typename T>
void A<T>::display()
{
    
    
    this->b = 10;
    cout << this->b << endl;
}
template <typename T>
void A<T>::print() const
{
    
    
}
template <typename T>
void func1()
{
    
    
    typename A<T>::c cc = 2;
    cout << cc << endl;
}

int main()
{
    
    
    A<int> a;
    typename std::vector<int> b;
    sort(b.begin(), b.end());
    func1<int>();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35140742/article/details/122731942