zoukankan      html  css  js  c++  java
  • C++ Template之函数模版

    函数模版的定义:

    template <typename T>
    T const& max(const T& a,const T b)
    {
    	return a > b ? a:b;
    }
    
    int main()
    {
    	return 0;
    }
    

     函数模版的使用:

    #include <iostream>
    #include <string>
    using namespace std;
    
    template <typename T>
    T const& max(const T& a,const T b)
    {
    	return a > b ? a:b;
    }
    
    int main()
    {
    	int i = 7;
    	cout << max(34,7) <<endl;
    	double j = 10.9;
    	cout << max(2.4,j) <<endl;
    	string s1 = "mathematics";
    	string s2 = "math";
    	cout << max(s1,s2)<<endl;
    	return 0;
    }
    

     函数模版参数的推断

     模版参数有显式指定和隐式推断两种,显示的不可以进行类型转换,隐士的不能进行类型转换,类型必须唯一。

    函数模版参数不能指定模版实参,这点和类模版不同。

    模版函数重载

    相同的情况下,先调用非模版函数

    如果模版函数有一个最佳的匹配则调用模版函数。

    显示指定空的模版参数列表指定从函数模版推导。

  • 相关阅读:
    linux基础名词
    计算机基础
    c++ 构造函数
    c++ stdafx.h、targetver.h文件
    centos7初步命令
    mysql 操作表的语句
    后台返回API数据格式
    nginx相关知识
    js复制内容到剪贴板格式化粘贴到excel中
    PHP开启错误提示
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3209828.html
Copyright © 2011-2022 走看看