1. 인자가 하나인 템플릿
2. 인자가 2개인 템플릿
3. fold_expression 사용한 템플릿
4. ??
1. 인자가 하나인 템플릿
2. 인자가 2개인 템플릿
3. fold_expression [폴드표현식]사용한 템플릿
4. ??
1. 인자가 하나인 템플릿
호출하는 쪽에서 인자의 자료형과 리턴자료형을 결정
#include<iostream>
#include<string>
using namespace std;
template <typename T>
T sum(T a, T b){
return a + b;
}
int main(void) {
int a=1, b =2;
double d1 = 2.2;
double d2 = 3.3;
string s1 = "Show me ";
string s2 = "The Money 6";
cout << "int 합 : " << sum<int>(a, b) << endl;
cout << "double 합 : " << sum<double>(d1, d2) << endl;
cout << "string 합 : " << sum<string>(s1, s2) << endl;
return 0;
}
2. 인자가 2개인 템플릿
template로 사용되는 인자가 2개 이상일 경우, <>로 자료형을 명시하지 않음
그 때에는 컴파일러가 스스로 자료형을 판단
#include <iostream>
#include <string>
#include <iostream>
#include <string>
using namespace std;
template <typename T1, typename T2>
void printAll(T1 a, T2 b){
cout << "T1 : " << a << endl;
cout << "T2 : " << b << endl;
};
int main(void){
string s1 = "Dok2" ;
string s2 = "On my way." ;
int num1 = 27;
int num2 = 35;
double d1 = 3.14;
double d2 = 36.5;
// template로 사용되는 인자가 2개 이상일 경우, <>로 자료형을 명시하지 않음
// 그 때에는 컴파일러가 스스로 자료형을 판단
cout << "[string, string]" << endl;
printAll(s1, s2); //printAll<string, string>(s1,s2);
cout << "[string, int]" << endl;
printAll(s1, num1); //printAll<string, int>(s1,num1);
cout << "[int, int]" << endl;
printAll(num1, num2);
cout << "[int, double]" << endl;
printAll(num1, d1);
cout << "[double, double]" << endl;
printAll(d1, d2);
cout << "[double, string]" << endl;
printAll(d1, s1);
return 0;
}
3. fold_expression [폴드표현식]사용한 템플릿
(Args... args) 설명 : 템플릿자료형에 가변인자(args)
ex)
// fold_test(1, 2, 3, 4, 5) 인 경우
template<typename... Args>
auto fold_test(Args... args) {
// ...
}
// --------- 상단과 하단은 동일한 코드 -----------
template<typename T1, typename T2, typename T3, typename T4, typename T5>
auto fold_test(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) {
// ...
}
fold_expression 설명 : 다수의 값을 인자로 받을
ex)
// 1 좌측 폴드
return (... - args); // 좌측 폴드를 사용하여 모든 args를 더함
// ex) (((((1 - 2) - 3) - 4) - 5) = -13
// 2 우측 폴드
return (args - ...); // 좌측 폴드를 사용하여 모든 args를 더함
// ex) (1 - (2 - (3 - (4 - 5)))) = 3
// 3 논리 AND 폴드 표현식
return (... && args); // 모든 인자가 true이면 true 반환
// ex) ((false && false) && true)
// 4 논리 OR 폴드 표현식
return (... || args); // 어떤 인자라도 true이면 true 반환
// ex) ((false || false) || true)
--- 전체 예제
#include <iostream>
template<typename... Args>
auto sum(Args... args) {
return (... + args); // 좌측 폴드를 사용하여 모든 args를 더함
}
template<typename... Args>
auto fold_test(Args... args) {
// ex) fold_test(1, 2, 3, 4, 5) 인 경우
// ---------------------------------------------------------------- //
// 1 좌측 폴드
return (... - args); // 좌측 폴드를 사용하여 모든 args를 더함
// ex) (((((1 - 2) - 3) - 4) - 5) = -13
// 2 우측 폴드
return (args - ...); // 좌측 폴드를 사용하여 모든 args를 더함
// ex) (1 - (2 - (3 - (4 - 5)))) = 3
// ex) fold_test(false, false, true); 인 경우
// ---------------------------------------------------------------- //
// 3 논리 AND 폴드 표현식
return (... && args); // 모든 인자가 true이면 true 반환
// ex) ((false && false) && true)
// 4 논리 OR 폴드 표현식
return (... || args); // 어떤 인자라도 true이면 true 반환
// ex) ((false || false) || true)
}
int main() {
// auto result = sum(1, 2, 3, 4, 5); // 15를 반환
// auto result = sum(1); // 1
// auto result = fold_test(1, 2, 3, 4, 5);
auto result = fold_test(false, false, true);
std::cout << "The result is " << result << std::endl;
return 0;
}
//// C++11 방식
//#include <iostream>
//
//using namespace std;
//
//template<typename T>
//constexpr auto tsum(T value) // 기본 함수
//{
// return value;
//}
//
//template<typename T, typename... Targs>
//constexpr auto tsum(T value, Targs... args) // 재귀 가변 함수
//{
//// cout << '1' << endl;
//// cout << args... << endl;
// return value - tsum(args...);
//}
//
//
//int main()
//{
// constexpr auto sum = tsum(1, 2, 3);
// cout << sum << '\n'; // 21
// return 0;
//}
'언어 정리 > C++_개념_lib' 카테고리의 다른 글
typedef (0) | 2024.01.03 |
---|---|
#if, 초기화리스트, const, namespace (0) | 2024.01.02 |
가변인자 (1) | 2023.12.27 |
Lvalue , Rvalue , 메모리 누수 (1) | 2023.12.27 |
참조와 포인터 (0) | 2023.12.26 |
댓글