一、类模板全特化、偏特化
#pragma once
#include <iostream>
#include <map>
template <typename T, typename U>
class TC
{
public:
TC()
{
std::cout << "泛化版本构造函数" << std::endl;
}
void funtest()
{
std::cout << "泛化版本成员函数" << std::endl;
}
};
template<>
class TC<int, int>
{
public:
TC()
{
std::cout << "全特化版本构造函数" << std::endl;
}
void funtest()
{
std::cout << "全特化版本成员函数" << std::endl;
}
};
template<>
void TC<double, double>::funtest()
{
std::cout << "全特化版本函数" << std::endl;
}
main.cpp
#include <iostream>
#include "template.h"
using namespace std;
int main()
{
TC<char, int> tchar;
tchar.funtest();
TC<int, int> tint;
tint.funtest();
TC<double, double> tdouble;
tdouble.funtest();
}
输出:
泛化版本构造函数
泛化版本成员函数
全特化

本文详细探讨了C++中的类模板全特化和偏特化,包括在不同情况下的实现和使用示例,如通过模板参数数量和范围进行偏特化,以及函数模板的全特化。内容涵盖了泛化版本的构造函数和成员函数,以及全特化版本的实现。
最低0.47元/天 解锁文章
5689

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



