C++ 函数模板

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;

template<typename T>   
T  sum(T other1, T other2) 
{
    T reslut = other1 + other2;
    return reslut;
}


int main()
{
    //使用函数模板可以实现通用的算法,不用重复写相同算法,类型不同的代码
    int reslut1 = sum<int>(1, 2);
    double reslut2 = sum<double>(1.1, 1.2);

    return 0;
}

 

posted @ 2020-04-11 20:20  神迹丶  阅读(160)  评论(0编辑  收藏  举报
网站已运行: