调用成员模板

C++访问成员模板需要加template关键字

#include <iostream>
using namespace std;
class A {
public:
template <typename TypeNum>
int getSize()
{
return sizeof(TypeNum);
}
};
// 调用成员模板函数需要加template
template <typename T>
void f1()
{
T t1;
cout << (t1.template getSize<float>()) << endl;
cout << (t1.template getSize<double>()) << endl;
}
// 调用成员模板函数不需要加template
void f2()
{
A a1;
cout << (a1.getSize<long>()) << endl;
cout << (a1.getSize<long long>()) << endl;
}
int main()
{
f1<A>();
f2();
return 0;
}
posted @   devin1024  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示