C++ 类模板、函数模板全特化、偏特化

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、类模板全特化、偏特化

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

输出:

泛化版本构造函数
泛化版本成员函数
全特化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值