#include<stdio.h>
#include<iostream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <climits>
#include <sstream>
#include <cstdlib>
using namespace std; /**
* 重载,函数有不同数目的参数或者是不同类型的参数
*/
int operate(int a, int b)
{
return (a * b);
}
/**
*注意只有返回类型不同不是重载
*/
double operate(double a, double b)
{
return (a / b);
}
/**
* 重载的函数可能会有同样的定义,比如下面这俩个函数
*/ int sum(int a, int b)
{
return a + b;
}
double sum(double a, double b)
{
return a + b;
} /**
* 泛型
* template <template-parameters> function-declaration
* parameter class type or typename type
*
*/
template<class someType> someType sum(someType a, someType b)
{
return a + b + 8;
}
/**
* In all cases, it represents a generic type that will be determined on the moment the template is instantiated.
* 代表泛型的类型在模版实列化的时候决定
*/
template<typename someType> someType sum2(someType a, someType b)
{
return a + b + 8;
}
/**
* Instantiating a template is applying the template to create a function using particular types or values for its template parameters
*实例模版指应用模版创建一个函数,使用特定的类型或者值转递给模版参数
*
*This is done by calling the function template,with the same syntax as calling a regular function,but specifying the template arguments enclosed in angle brackets:
*由正在被调用的函数模版来完成,就像调用一个普通函数一样,但是需要在尖括号内指定模版参数
*如下
*name <template-arguments>(function-arguments)
*x=sum<int>(10,20)
*
*The function sum<int> is just one of the possible instantiations of function template sum. In this case, by using int as template argument in the call, the compiler automatically instantiates a version of sum where each occurrence of SomeType is replaced by int, as if it was defined as:
*
*编译器自动生成实例化一个int版本的sum函数,如下
*
*int sum (int a, int b)
*{
* return a+b;
*}
*/
template<typename T, typename R> T fs(T t, R r)
{
return r + t;
}
template<class T, class U>
bool are_equal(T a, U b)
{
return (a == b);
}
/**
*无类型模版参数
*/
template<class T, int N> T fixed_multiply(T val)
{
return val * N;
}
int main()
{
int x = 5, y = 2;
double n = 5.1111, m = 2.0;
//cout << operate(x, y) << '\n';
//cout << operate(n, m) << '\n';
//模版
int k = sum<int>(x, y);
cout << k << endl;
double dk = sum<double>(n, m);
cout << dk << endl;
//注意这个和sum<double>
dk = sum(n, m);
//without the type enclosed in angle brackets. Naturally, for that, the type shall be unambiguous. If sum is called with arguments of different types, the compiler may not be able to deduce the type of T automatically.
//对上面sum的解释,如果尖括号内没有写类型,实际上表示类型任意,如果sum再被调用时参数是不同的类型,编译器可能不能自动推测出T的类型
cout << dk << endl;
//把sum函数注释,下面这句话会导致编译不过
//dk = sum(x,n);
//注意这俩个
int kkk = fs<double, int>(n, x);
// double kkk = fs<double,int>(n,x);
cout << kkk << endl; if(are_equal(10, 10.0))
cout << "x and y are equal\n";
else
cout << "x and y are not equal\n";
//are_equal(10,10.0)和are_equal<int,double>(10,10.0)一样
//没有任意的可能性,因为数字字面量总是会指定类型, integer literals always produce values of type int, and floating-point literals always produce values of type double. Therefore 10 has always type int and 10.0 has always type double. //无类型模版参数
cout<<fixed_multiply<int,2>(10)<<endl;
cout<<fixed_multiply<int,3>(10)<<endl;
/**
* 上面和template<class int,class int>有个主要的区别
*But there exists a major difference: the value of template parameters is determined on compile-time to generate a different instantiation of the function fixed_multiply, and thus the value of that argument is never passed during runtime: The two calls to fixed_multiply in main essentially call two versions of the function: one that always multiplies by two, and one that always multiplies by three. For that same reason, the second template argument needs to be a constant expression (it cannot be passed a variable).
*
*但是这存在一个主要的区别,模版的参数在编译时被确定用于生成不同fixed_multiply的实例
*所以,参数的值永远都不会在运行时传递.在main里面调用的俩个fixed_multiply本质上
*调用俩个不同版本的函数,一个总是乘2,一个总是乘3.因此,模版上第二个参数需要是一个常量表达式(它不能传递变量)
*
*int kkkk = 2 or const int kkkk =2;
*cout<<fixed_multiply<int,3>(10)<<endl;
*/
//error
// int kkkk = 4 ;
// cout<<fixed_multiply<int,kkkk>(10)<<endl;
//ok
const int kkkk2 =20;
cout<<fixed_multiply<int,kkkk2>(10)<<endl;
return 0;
}

  

c++官方文档-模版函数和重载的更多相关文章

  1. c++官方文档-模版类

    #include <iostream> using namespace std; template<class T> class MyPair { private: T t[] ...

  2. swift官方文档中的函数闭包是怎么理解的?

    官方文档中的16页: numbers.map({ (number: Int) -> Int in let result = * number return result }) 不知道这个怎么用, ...

  3. tensorflow官方文档中的sub 和mul中的函数已经在API中改名了

    在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...

  4. 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)

    上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...

  5. Android的AutoCompleteTextView在API17高版本添加的setText函数在低版本系统居然能正常调用?官方文档是不是不靠谱了?

    官方文档:https://developer.android.com/reference/android/widget/AutoCompleteTextView.html#setText(java.l ...

  6. Kotlin开发语言文档(官方文档)-- 目录

    开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...

  7. OpenGL ES着色器语言之语句和结构体(官方文档第六章)内建变量(官方文档第七、八章)

    OpenGL ES着色器语言之语句和结构体(官方文档第六章) OpenGL ES着色器语言的程序块基本构成如下: 语句和声明 函数定义 选择(if-else) 迭代(for, while, do-wh ...

  8. python3 asyncio官方文档中文版

    事件循环基类 事件循环基类 事件循环是由asyncio提供的核心执行装置.它提供了多种服务,包括: 注册.执行和关闭延时调用(超时) 为各种通信创建客户端和服务端传输 为一个外部程序通信启动子进程和相 ...

  9. 《Apache Velocity用户指南》官方文档

    http://ifeve.com/apache-velocity-dev/ <Apache Velocity用户指南>官方文档 原文链接   译文连接 译者:小村长  校对:方腾飞 Qui ...

随机推荐

  1. TJU Problem 1090 City hall

    注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型. 原题: 1090.   City hall Time Limit: 1.0 Seconds   Memory ...

  2. tesseract text2image windows

    text2image.exe --text="X:\xxx.txt" --outputbase="X:\test.font.exp0" --fontconfig ...

  3. stm32 DMA配置

    DMA就是将一个地址空间复制到另外一个地址空间.DMA传输方式无需CPU直接控制传输,也没有中断处理方式那样保留现场和恢复现场的过程,通过硬件为RAM与I/O设备直接传送数据,使CPU的效率大大的提高 ...

  4. win7上搭建Android环境及调试

    工欲善其事必先利其器,好记性不如烂笔头.要学习一门新的语言,首先必须得先搭环境,否则没法实践.如果之前按照网上的提示,搭建过环境,而且环境比较复杂的话,我相信隔很长一段时间后,就会忘记,到真正用的时候 ...

  5. JSP+JavaBean+Servlet技术(MVC模型)

    一,Servlet开发用户在浏览器中输入一个网址并回车,浏览器会向服务器发送一个HTTP请求.服务器端程序接受这个请求,并对请求进行处理,然后发送一个回应.浏览器收到回应,再把回应的内容显示出来.这种 ...

  6. C/C++基础----类

    IO类属于不能被拷贝的类型,因此只能通过引用来传递.同时读取和写入操作都会改变流的内容,所以接收的是普通引用. 类内的友元声明仅仅指定了访问的权限,需要在友元声明之外再专门对函数进行一次声明. 可变数 ...

  7. bzoj1034 泡泡堂

    Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵 ...

  8. HttpUrlConnection使用详解--转

    http://blog.csdn.net/fightingXia/article/details/71775516 一,HttpURLconnection的介绍 在Android开发中网络请求是最常用 ...

  9. Bezier画线算法

    编译器:VS2013 描述:Bezier画线是利用导数相同拼接曲线,使曲线十分光滑,而不是随意拼接观赏性很差 主函数段 #include "stdafx.h" #include&l ...

  10. loadrunner怎么解决录制完成后脚本为空

    第一步: 第二步: 设置完后就Ok了