8. 템플릿
1. type_traits 1
#include <iostream>
using namespace std;
enum TempEnum {};
enum class TempEnumClass : int {};
int main() {
cout << boolalpha;
cout << "== is_integral ==" << endl;
cout << is_integral<TempEnum>::value << ", ";
cout << is_integral<TempEnumClass>::value << ", ";
cout << is_integral<signed int>::value << ", ";
cout << is_integral<unsigned int>::value << ", ";
cout << is_integral<double>::value << ", ";
cout << is_integral<bool>::value << endl
<< endl;
cout << "== is_enum ==" << endl;
cout << is_enum<TempEnum>::value << ", ";
cout << is_enum<TempEnumClass>::value << ", ";
cout << is_enum<int>::value << endl
<< endl;
cout << "== is_signed ==" << endl;
cout << is_signed<TempEnum>::value << ", ";
cout << is_signed<signed int>::value << ", ";
cout << is_signed<unsigned int>::value << endl
<< endl;
cout << "== is_unsigned ==" << endl;
cout << is_unsigned<TempEnumClass>::value << ", ";
cout << is_unsigned<int>::value << ", ";
cout << is_unsigned<unsigned int>::value << endl;
return 0;
}
2. type_traits 2
#include <iostream>
#include <map>
using namespace std;
struct TempStruct {
int x = 0;
};
class TempClass {};
int main() {
cout << boolalpha;
cout << "== is_empty ==" << endl;
cout << is_empty<TempStruct>::value << ", ";
cout << is_empty<TempClass>::value << endl
<< endl;
;
cout << "== is_array ==" << endl;
cout << is_array<TempStruct>::value << ", ";
cout << is_array<map<int, double>>::value << ", ";
cout << is_array<int[3]>::value << endl
<< endl;
cout << "== is_same ==" << endl;
cout << is_same<TempStruct, TempClass>::value << ", ";
cout << is_same<int, signed int>::value << ", ";
cout << is_same<int, bool>::value << ", ";
cout << is_same<char, unsigned char>::value << endl;
return 0;
}
3. type_traits 3
#include <iostream>
#include <string>
using namespace std;
template <typename T1, typename T2>
string TempFunc(T1 x, T2 y) {
using type1 = typename conditional<is_same<T1, T2>::value, int, double>::type;
return typeid(type1).name();
}
int main() {
auto number1 = 1;
auto number2 = 22.3;
string type = TempFunc(number1, number2);
cout << "number2는 ";
if (type == "i")
cout << "int 자료형입니다." << endl;
else
cout << "int 자료형이 아닙니다." << endl;
cout << "number2는 ";
if (type == "d")
cout << "double 자료형입니다." << endl;
else
cout << "double 자료형이 아닙니다." << endl;
return 0;
}
4. 함수 템플릿
#include <iostream>
using namespace std;
template <typename T>
T Plus(T x, T y, T z = 1) {
return x + y + z;
}
int main() {
int number1 = Plus(1, 2);
int number2 = Plus(1, 2, 3);
double number3 = Plus(1.1, 2.2);
double number4 = Plus(1.1, 2.2, 3.3);
cout << "결과값: " << number1 << ", " << number2
<< ", " << number3 << ", " << number4 << endl;
return 0;
}
5. 클래스 템플릿
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
class TemplateData {
private:
vector<T> data_;
public:
void Add(T arg) { this->data_.push_back(arg); }
int Size() { this->data_.size(); }
void Print() {
for (int i = 0, size = this->data_.size(); i < size; i++)
cout << "data_: " << this->data_.at(i) << endl;
}
};
int main() {
TemplateData<int> data_int = TemplateData<int>();
data_int.Add(1);
data_int.Add(2);
data_int.Print();
TemplateData<string> data_string = TemplateData<string>();
data_string.Add("Hello");
data_string.Add("world");
data_string.Add("!!");
data_string.Print();
return 0;
}
6. 가변인자 함수 템플릿
#include <iostream>
#include <string>
using namespace std;
void Func1(int no, double value) {
cout << no << ", 오류값 " << value << endl;
}
void Func2(string msg, int no, double value) {
cout << msg << ", 에러 번호 "
<< no << ", 오류값 " << value << endl;
}
template <typename... T>
void PrintLog(T... args) {
Func1(args...);
// Func2(args...);
}
int main() {
PrintLog<int, double>(100, 22.5);
// PrintLog<string, int, double>("Warning", 100, 22.5);
return 0;
}