본문 바로가기

PROGRAMMING LANGUAGE/C++

템플릿 특수화

#include <iostream>  
using namespace std;


// 템플릿 
template <typename T1, typename T2>
class MySimple
{
public:
	void WhoAreYou()
	{
		cout << "size of T1: " << sizeof(T1) << endl;
		cout << "size of T2: " << sizeof(T2) << endl;
		cout << "<typename T1, typenmae T2>" << endl;
	}
};

// 템플릿 특수화
template<>
class MySimple<int, double>
{
public:
	void WhoAreYou()
	{
		cout << "size of int: " << sizeof(int) << endl;
		cout << "size of double: " << sizeof(double) << endl;
		cout << "<typename int, typenmae double>" << endl;
	}
};

// 템플릿 부분특수화
template<typename T1>
class MySimple<T1, double>
{
public:
	void WhoAreYou()
	{
		cout << "size of T1: " << sizeof(T1) << endl;
		cout << "size of double: " << sizeof(double) << endl;
		cout << "<typename T1, typenmae double>" << endl;
	}
};

int main(void)
{
	// 부분특수화
	MySimple<char, double> obj1; obj1.WhoAreYou();
	// 템플릿
	MySimple<int, long> obj2; obj2.WhoAreYou();
	// 템플릿 특수화
	MySimple<int, double> obj3; obj3.WhoAreYou();

	return 0;
}

'PROGRAMMING LANGUAGE > C++' 카테고리의 다른 글

cin.getline string getline  (0) 2021.12.07
문제풀이 1  (0) 2021.12.07
템플릿 함수 사용하기  (0) 2021.12.02
상속과 예외처리의 관계  (0) 2021.12.02
c++예외처리  (0) 2021.12.01