C++

[C++ 5-4] 함수 템플릿

KimKyao 2024. 10. 21. 20:30
#include <iostream>

using namespace std;

//class 혹은 typeName
template <class Any>
Any sum(Any, Any);

template <class Any>
Any sum(int, Any);

int main(){   
	// 함수 템플릿
	// 구체적인 데이터형을 포괄할 수 있는 일반형으로 함수를 정의
	int a = 3;
	int b = 4;
	//cout << sum(a, b) << endl;// 에러,

	float c = 3.111;
	float d = 4.666;
	cout << sum(c, d) << endl;

	//cout << sum(a, c) << endl; 
	cout << sum(a, c) << endl; //
	return 0;
}


template <class Any>
Any sum(Any a, Any b) {
	return a + b;
}

template <class Any>
Any sum (int a, Any b) {
	return a + b;
}