函数模板

1、函数模板举例

template <typename T>
void Swap(T& arg1, T& arg2)
{
    T temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

template <typename T>
void Swap(T* arg1, T* arg2, int size)
{
    for (int i = 0; i < size; ++i)
    {
        T temp = arg1[i];
        arg1[i] = arg2[i];
        arg2[i] = temp;
    }
}

int main()
{
    int a = 0, b = 1;
    Swap(a, b);
    int c[10] = { 0 }, d[10] = { 0 };
    Swap(c, d, 10);
    return 0;
}

函数模板支持重载,例子中Swap(a, b)Swap(c, d, 10)调用的函数模板就不一样。

#include <iostream>

template <typename T, typename U>
void Func(T& arg1, U& arg2)
{
    std::cout << arg1 << " TU " << arg2 << std::endl;
}

template <typename U>
void Func(int& arg1, U& arg2)
{
    std::cout << arg1 << " intU " << arg2 << std::endl;
}

int main()
{
    int a = 0, b = 1;
    Func(a, b);                     //0 intU 1

    double c = 1.0, d = 1.1;
    Func<double>(c, d);             //1 TU 1.1
    return 0;
}

上面是另外一种重载函数模板的例子。

2、函数模板显式具体化

模板显式具体化又称特例化,类模板可偏特化(部分特例化)或全特化,但函数模板只能全特化,不能偏特化。
函数模板支持编译器自动推导模板参数类型,但是类模板不支持自动推导。

#include <iostream>

template <typename T>
void Swap(T& arg1, T& arg2)
{
    T temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

struct MyStruct
{
    int a;
    short b;
    MyStruct(int _a, int _b)
    {
        a = _a;
        b = _b;
    }
    void Show()
    {
        std::cout << "a:" << a << " b:" << b << std::endl;
    }
};

int main()
{
    MyStruct stu1(1, 1);
    MyStruct stu2(2, 2);
    stu1.Show();        //a:1 b:1
    stu2.Show();        //a:2 b:2
    Swap(stu1, stu2);
    stu1.Show();        //a:2 b:2
    stu2.Show();        //a:1 b:1
    return 0;
}

由于模板函数Swap采用了默认模板,如果我们想Swap只交换成员a的值而不交换成员b的值,则需要显式具体化一个MyStruct专属的函数。

#include <iostream>

template <typename T>
void Swap(T& arg1, T& arg2)
{
    T temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

struct MyStruct
{
    int a;
    short b;
    MyStruct(int _a, int _b)
    {
        a = _a;
        b = _b;
    }
    void Show()
    {
        std::cout << "a:" << a << " b:" << b << std::endl;
    }
};

template<>
//void Swap<MyStruct>(MyStruct& arg1, MyStruct& arg2)       //这两种写法都可以
void Swap(MyStruct& arg1, MyStruct& arg2)
{
    int temp = arg1.a;
    arg1.a = arg2.a;
    arg2.a = temp;
}


int main()
{
    MyStruct stu1(1, 1);
    MyStruct stu2(2, 2);
    stu1.Show();        //a:1 b:1
    stu2.Show();        //a:2 b:2
    Swap(stu1, stu2);
    stu1.Show();        //a:2 b:1
    stu2.Show();        //a:1 b:2
    return 0;
}
函数调用优先级

非模板函数>显式具体化函数>普通模板函数

3、实例化和具体化

代码中包含函数模板本身并不会生成函数定义,它只是一个用于生成函数定义的方案。
编译器使用模板为特定内省生成函数定义时,得到的是模板实例,这种实例化方式称为隐式实例化。

template <typename T>
void Swap(T& arg1, T& arg2)
{
    T temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}

int main()
{
    int a = 0, b = 1;
    Swap(a, b);             //隐式实例化

    double c = 1.0, d = 1.1;
    Swap<double>(c, d);     //显式实例化
    return 0;
}

在声明中使用前缀template<>template区分显式具体化和显式实例化。

显式具体化、隐式实例化、显式实例化统称为模板实例化。
前面讲到函数模板支持编译器自动推导模板参数类型,但是类模板不支持自动推导。
隐式实例化就是让编译器自动推导模板参数类型,而显式实例化就是直接告诉编译器模板参数。

4、缺省模板参数

#include <iostream>

template <typename T = int, typename U>
//template <typename T, typename U = int>       //这两种方法都可以
void Func(T& arg1, U& arg2)
{
    std::cout << arg1 << " " << arg2 << std::endl;
}

int main()
{
    int a = 0, b = 1;
    Func(a, b);                     //0 1

    double c = 1.0, d = 1.1;
    Func<double>(c, d);             //1 1.1
    return 0;
}

函数模板的缺省参数可以是模板参数中任意的参数。

5、非类型模板参数

#include <iostream>

template <typename T = int, typename U, int i>
void Func(T arg1, U arg2)
{
    std::cout << arg1 << " " << arg2 << " " << i <<std::endl;
    U a[i];     //i是常量,所以用来创建数组
}

int main()
{
    int a = 0, b = 1;
    Func<int, int, 10>(a, b);       //0 1 10
    Func<double, int, 5>(1.1, 1);   //1.1 1 5
    return 0;
}

非类型模板参数也可以有默认值。

#include <iostream>

template <typename T = int, typename U, U i = 10>
void Func(T arg1, U arg2)
{
    std::cout << arg1 << " " << arg2 << " " << i <<std::endl;
    U a[i];     //i是常量,所以可被用来创建数组
}

int main()
{
    int a = 0, b = 1;
    Func(a, b);                 //0 1 10
    Func<double, int, 5>(1.1, 1);   //1.1 1 5
    return 0;
}

非类型模板参数的类型比较有限,暂时只有整形、枚举、指针、左值引用、autodecltype等。

6、可变参数模板

变参模板就是一个接受可变数目参数的模板函数或模板类。可变数目的参数被称为参数包。
存在两种参数包:模板参数包,表示零个或多个模板参数;函数参数包,表示零个或多个函数参数。

template <typename T, typename ... Args>
void fun(const T& t, const Args& ... args);

Args是一个模板参数包,表示零个或多个模板类型参数。args是一个函数参数包,表示零个或多个函数参数。

获取参数包中参数个数
#include <stdio.h>

template <typename T, typename ... Args>
void fun(const T& t, const Args& ... args)
{
    printf("%d %d\n", sizeof ...(Args), sizeof ...(args));
}

int main()
{
    fun(1, 2, 3);       //2 2
    return 0;
}

上述函数参数包就有两个模板类型参数、两个函数参数。

编写可变参数函数模板

可变参数函数通常是递归的,第一步调用处理参数包中的第一个实参,然后用剩余实参调用自身。
为了终止递归还需要定义一个非可变参数的函数,如下代码示例。

#include <iostream>
#include <string>

template <typename T>
std::ostream& myprint(std::ostream& os, const T& t)
{
    return  os << t;
}

template <typename T, typename ... Args>
std::ostream& myprint(std::ostream& os, const T& t, const Args& ... args)
{
    os << t << ", ";
    return myprint(os, args...);
}

int main()
{
    std::string str = "Hello World";
    myprint(std::cout, 1, 2, 3, str);       //1, 2, 3, Hello World
    return 0;
}
转发可变参数模板

这个其实跟递归一个道理,递归就是可变参数模板的自我转发。
下面例子中的myprint2就将参数包转发给了myprint

#include <iostream>
#include <string>

template <typename T>
std::ostream& myprint(std::ostream& os, const T& t)
{
    return  os << t;
}

template <typename T, typename ... Args>
std::ostream& myprint(std::ostream& os, const T& t, const Args& ... args)
{
    os << t << ", ";
    return myprint(os, args...);
}

template <typename T, typename ... Args>
std::ostream& myprint2(std::ostream& os, const T& t, const Args& ... args)
{
    return myprint(os, t, args...);
}

int main()
{
    std::string str = "Hello World";
    myprint2(std::cout, 1, 2, 3, str);      //1, 2, 3, Hello World
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值