函数模板与类模版

+ 概述
  1. typename 是用来定义模板参数关键字,也可以使用 class.
  2. 用不同类型的参数使用函数模板时, 称为函数模板的实例化.
函数模版
类模版
#include <iostream>

template <typename T>
T max(T a, T b) {
    return a > b ? a : b;
}

struct Test{
    int v_{};
    Test() = default;
    Test(int v) :v_(v) {}
    bool operator>(const Test& t) const{
        return this->v_ > t.v_;
    }
};

int main(){
    int a{ 1 };
    int b{ 2 };
    // ::max();  表示调用的是自己写的 max() 函数(:: 是作用域解析运算符,表示在全局命名空间中查找标识符 max).
    // std::max();  表示调用的是标准模板库的 max() 函数
    std::cout << "max(a, b) : " << ::max(a, b) << '\n';

    Test t1{ 10 };
    Test t2{ 20 };
    std::cout << "max(t1, t2) : " << ::max(t1, t2).v_ << '\n';

}
#include <iostream>

template <typename T>
class Max {
public:
    T max1(T a, T b) {
        return (a > b) ? a : b;
    }
    T max2(T a, T b);
};

// 类模板中函数放在类外进行定义时,需要加模板参数列表
template <typename T>
T Max<T>::max2(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    Max<int> intMax;
    std::cout << "Max of 10 and 20: " << intMax.max1(10, 20) << std::endl;

    Max<double> doubleMax;
    std::cout << "Max of 3.5 and 8.2: " << doubleMax.max2(3.5, 8.2) << std::endl;

    return 0;
}

模版的特化

+ 概述
  1. 特化:为特定类型或参数提供特定的实现,而不是使用通用的模板定义.
函数模版显式特化
类模版显式特化
#include <iostream>
#include <cstring>

// 通用的函数模板,返回两个数中较大的数
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

// 显式特化,针对字符串类型的特化实现,返回长度较长的字符串
template <>
const char* max<const char*>(const char* a, const char* b) {
    return std::strlen(a) > std::strlen(b) ? a : b;
}

int main() {
    // 调用通用模板实现
    std::cout << "Max of 10 and 20: " << max(10, 20) << std::endl;
    // 使用字符串类型特化实现
    std::cout << "Max of 'hello' and 'world': " << max("hello", "world") << std::endl;
    return 0;
}
#include <iostream>
#include <cstring>
// 通用的类模板
template <typename T>
class Max {
public:
    T max(T a, T b) {
        return (a > b) ? a : b;
    }
};

// 类模板的显式特化,针对const char*指针类型
template <>
class Max<const char*> {
public:
    const char* max(const char* a, const char* b) {
        return std::strlen(a) > std::strlen(b) ? a : b;
    }
};

int main() {
    Max<int> intMax;

    // 调用通用模板实现
    std::cout << "Max of 10 and 20: " << intMax.max(10, 20) << std::endl;

    Max<const char*> charMax;
    
    // 使用字符串类型特化实现
    std::cout << "Max of 'hello' and 'world': " << charMax.max("hello", "world") << std::endl;

    return 0;
}

全特化与偏特化

+ 概述
  1. 全特化: 全特化是指为模板的所有模板参数提供特定的实现,即将模板参数列表中所有的参数都确定化.
  2. 偏特化: 部分特化,即在模板参数中指定一部分参数的特定类型,而另一部分参数保持通用.偏特化可以分为类模板偏特化和函数模板偏特化
全特化
偏特化
#include <iostream>

// 通用的函数模板
template <typename T>
class MyType {
public:
    void print() {
        std::cout << "Generic type" << std::endl;
    }
};

// 全特化,针对int类型的全特化
template <>
class MyType<int> {
public:
    void print() {
        std::cout << "Specialized type for int" << std::endl;
    }
};

int main() {
    MyType<double> obj1;
    obj1.print();

    MyType<int> obj2;
    obj2.print();

    return 0;
}
#include <iostream>

// 通用的类模板
template <typename T, typename U>
class MyType {
public:
    void print() {
        std::cout << "Generic type" << std::endl;
    }
};

// 类模板的偏特化,针对int类型和double类型的偏特化
template <typename U>
class MyType<int, U> {
public:
    void print() {
        std::cout << "Partially specialized type for int" << std::endl;
    }
};

// 函数模板的偏特化,针对指针类型的偏特化
template <typename T>
void func(T* ptr) {
    std::cout << "Generic function template" << std::endl;
}

template <>
void func(int* ptr) {
    std::cout << "Partially specialized function template for int" << std::endl;
}

int main() {
    MyType<char, double> obj1;
    obj1.print();

    MyType<int, double> obj2;
    obj2.print();

    int x = 10;
    double y = 20.5;
    func(&x);
    func(&y);

    return 0;
}
+ 在线测试

链接

1.可变参数函数