函数模板 function template

函数模板 function template

//错误代码
#include <iostream>
using namespace std;

template <typename T>
double add(T a,T b,T c){
	return a+b+c;
}
int main(){
	double result=add(2,2,2.5);
	cout<<result;
} 

T为类型函数,不指定具体的类而是当函数调用的时候根据实参类型来确定T的类型。
T不能同时为int型又为double型。
所以以上代码报错:deduced conflicting types for parameter’T’('int’and ‘double’)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43036613/article/details/83064197