模板的特化

模板的特化

模板特化是C++模板编程中极其重要机制,模板特化就是模板编译时多态实现。
特化版本和普通版本拥有不同的外部接口和实现
偏特化:对部分模板参数进行特化。
支持普通版本,特化版本,偏特化版本

//template_special_vectorbool.cpp
vector对bool的特化处理,和别的细节差别

普通特化

#include <iostream>
using namespace std;

template <typename T> 
class Array {
private:
	T* ptr;
	int size;

public:
	Array(T arr[], int s);
	void print();
};

template <typename T> 
Array<T>::Array(T arr[], int s)
{
	ptr = new T[s];
	size = s;
	for (int i = 0; i < size; i++)
		ptr[i] = arr[i];
}

template <typename T> 
void Array<T>::print()
{
	for (int i = 0; i < size; i++)
		cout << *(ptr + i)<<" ";
	cout << endl;
}

template<> //表示特化版本——针对bool特殊处理
void Array<bool>::print()
{
	for (int i = 0; i < size; i++)
		cout << *(ptr + i);
	cout << endl;
}

int main()
{
	int arr[5] = { 1, 2, 3, 4, 5 };
	Array<int> a1(arr, 5);
	a1.print();

    bool boo[5] = { true, false, true, false, true };
    Array<bool> a2(boo, 5);	//如果没有void Array<bool>::print(),就是通用版本
    a2.print();
}

//编译执行如下
kongcb@tcu-pc:~/testcode/template$ g++ template_special.cpp -o template_special
kongcb@tcu-pc:~/testcode/template$ ./template_special
1 2 3 4 5 
10101

偏特化

#include <iostream>
#include <memory>
using namespace std;

template <typename U, typename V> //表示通用版本
struct Printer{
    U x;
    V y;

    void print()
    {
        cout<<x<<","<<y<<endl;
    }
};

template <> //特化版本——普通特化
struct Printer<int,int>{
    int x;
    int y;

    void print()
    {
        cout<<"["<<x<<","<<y<<"]"<<endl;
    }
};

template <> //特化版本——普通特化
struct Printer<double,double>{
    double x;
    double y;

    void print()
    {
        cout<<"{"<<x<<","<<y<<"}"<<endl;
    }
};

template <typename V> //偏特化(部分特化)
struct Printer<int,V>{	//U特化为int
    int x;
    V y;

    void print()
    {
        cout<<"["<<x<<"] "<<y<<endl;
    }
};

template <typename U, typename V> //指针特化
struct Printer<U*, V*> {
    U* x;
    V* y;

    void print()
    {
        cout<<x<<"->"<<*x<<endl;
        cout<<y<<"->"<<*y<<endl;
    }
};

int main()
{
    Printer<string, string> p1{"C++", "Java"};
    p1.print();

    Printer<int, int> p2{100,200};
    p2.print();

    Printer<double, double> p3{10.2,20.3};
    p3.print();

    Printer<int, string> p4{100,"C++"};
    p4.print();

    auto i1=make_unique<int>(100);
    auto i2=make_unique<int>(200);
    Printer<int*, int*> p5{i1.get(),i2.get()};
    p5.print();
}

//编译执行
kongcb@tcu-pc:~/testcode/template$ g++ template_special_part.cpp -o template_special_part
kongcb@tcu-pc:~/testcode/template$ ./template_special_part
C++,Java
[100,200]
{10.2,20.3}
[100] C++
0x229e030->100
0x229e050->200

vector对bool特殊特化

#include <vector>
#include <iostream>
using namespace std;

void zero(auto& container){
    for(auto&& val: container){	//取container元素
        val={};	//置0
    }
}

void print(auto& container){
    for(auto&& val: container){
        cout<<val<<" ";
    }
    cout<<endl;
}

int main()
{
    vector<int> vc={1,2,3};
    print(vc);
    zero(vc);
    print(vc);

    int& c=vc[0];	//可以得到地址
    vector<bool> vb={true,false,true};//vector<bool>实际做了一个特化,一个字节存8个bool量
    print(vb);//不可以左值,但可以取右值
    zero(vb);
     print(vb);
	//bool& b=vb[0];	//不可以取地址
}

//编译执行
kongcb@tcu-pc:~/testcode/template$ g++ template_special_vectorbool.cpp -o template_special_vectorbool -fconcepts-ts
kongcb@tcu-pc:~/testcode/template$ ./template_special_vectorbool
1 2 3 
0 0 0 
1 0 1 
0 0 0 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术的微光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值