【cpp】模板函数 模板类 特化 书写格式备忘

outline

收集的关于使用 模板函数 模板类 特化 偏特化的笔记 用以快速回忆

模板类

普通使用 类内定义成员函数

#include <iostream>
using namespace std;

template <typename T>
class MyContainer {
private:
    T element;

public:
    MyContainer(T elem) : element(elem) {}
    
    void display() {
        cout << "Element: " << element << endl;
    }
};
int main() {
    MyContainer<int> intContainer(5);
    intContainer.display(); 

    MyContainer<string> stringContainer("Hello");
    stringContainer.display(); 

    return 0;
}

在这里插入图片描述

普通使用 类外定义成员函数

#include <iostream>
using namespace std;

template <typename T>
class MyContainer {
private:
    T element;

public:
    MyContainer(T elem) : element(elem) {}

    void display();
};


template <typename T>
void MyContainer<T>::display() {
    cout << "Element: " << element << endl;
}

int main() {
    MyContainer<int> intContainer(5);
    intContainer.display(); 

    MyContainer<string> stringContainer("Hello");
    stringContainer.display(); 

    return 0;
}

在这里插入图片描述

类的全特化

#include <iostream>
using namespace std;

template <typename T>
class MyContainer {
private:
    T element;

public:
    MyContainer(T elem) : element(elem) {}

    void display();
};


template <typename T>
void MyContainer<T>::display() {
    cout << "Element: " << element << endl;
}

template <>
class MyContainer<int> {
private:
    int element;

public:
    MyContainer(int elem) : element(elem) {}
    void display() {
        cout << "Element is an integer: " << element << endl;
    }
};

int main() {
    MyContainer<int> intContainer(5);
    intContainer.display(); 

    MyContainer<double> doubleContainer(3.14);
    doubleContainer.display(); 

    return 0;
}

在这里插入图片描述

类的偏特化

#include <iostream>
using namespace std;

template <typename T, typename U>
class MyPair {
private:
    T first;
    U second;

public:
    MyPair(T f, U s) : first(f), second(s) {}
    void display() {
        cout << "First: " << first << ", Second: " << second << endl;
    }
};

// 偏特化为第二个参数为 int 的情况
template <typename T>
class MyPair<T, int> {
private:
    T first;
    int second;

public:
    MyPair(T f, int s) : first(f), second(s) {}
    void display() {
        cout << "First: " << first << ", Second (int): " << second << endl;
    }
};

int main() {
    MyPair<string, double> myPair1("Hello", 3.14);
    myPair1.display();  

    MyPair<string, int> myPair2("World", 10);
    myPair2.display();  

    return 0;
}


在这里插入图片描述

特别说明

当在模板类的成员函数中调用其他模板成员函数时。
当需要指定特定的模板参数时。
要把

doubleContainer.display();

写成

doubleContainer.template display();
#include <iostream>
using namespace std;

template <typename T>
class MyContainer {
private:
    T element;

public:
    MyContainer(T elem) : element(elem) {}

    // 模板成员函数
    template <typename U>
    U add(U value) {
        return element + value; // 这里假设 element 可以与 U 类型相加
    }

    void display() {
        cout << "Element: " << element << endl;
    }
};

int main() {
    MyContainer<int> intContainer(5);
    intContainer.display(); 

    // 调用模板成员函数
    cout << "Sum: " << intContainer.template add<double>(2.5) << endl; //使用 template 关键字

    return 0;
}

在这里插入图片描述

函数模板的全特化

函数模板没有偏特化,可以通过重载函数来处理不同的参数类型或数量,从而模拟偏特化的效果,理由是为了避免复杂性和潜在的歧义性,同时通过重载和全特化提供了足够的灵活性和功能。

函数的全特化

#include <iostream>
using namespace std;

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

template <>
int add<int>(int a, int b) {
    cout << "Using specialized version for int" << endl;
    return a + b;
}

int main() {
    cout << "Sum of integers: " << add<int>(3, 4) << endl; 
    cout << "Sum of doubles: " << add<double>(3.5, 2.5) << endl;

    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值