일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 포토샵
- Design Pattern
- Flexbox
- CSS
- stl
- spring
- 알고리즘
- pandas
- 백준
- Codility
- Photoshop
- 강화학습
- c
- 수학
- align-items
- Javascript
- Prefix Sums
- 소수
- 미디어 쿼리
- 확률
- 에라토스테네스의 체
- SK바이오사이언스
- 상태
- grid
- margin
- 통신사할인
- dataframe
- Gap
- c++
- skt membership
Archives
- Today
- Total
sliver__
[Mastering C++ Programming] - Partial template specialization 본문
728x90
- 기본 템플릿 클래스를 특정 데이터 유형에 대한 자체 전체 정의로 대체하는 명시적 템플릿 특수화와 달리 부분 템플릿 특수화를 사용하면 기본 템플릿 클래스에서 지원하는 템플릿 매개변수의 특정 하위 집합을 특수화할 수 있습니다.
- 반면 다른 일반 유형은 기본 템플릿 클래스와 동일합니다.
- 부분 템플릿 특수화가 상속과 결합된 예제는 아래와 같습니다.
#include <iostream>
using namespace std;
template <typename T1, typename T2, typename T3>
class MyTemplateClass {
public:
void F1( T1 t1, T2 t2, T3 t3 ) {
cout << "\nPrimary Template Class - Function F1 invoked ..." << endl;
cout << "Value of t1 is " << t1 << endl;
cout << "Value of t2 is " << t2 << endl;
cout << "Value of t3 is " << t3 << endl;
}
void F2(T1 t1, T2 t2) {
cout << "\nPrimary Tempalte Class - Function F2 invoked ..." << endl;
cout << "Value of t1 is " << t1 << endl;
cout << "Value of t2 is " << 2 * t2 << endl;
}
};
template <typename T1, typename T2, typename T3>
class MyTemplateClass< T1, T2*, T3*> : public MyTemplateClass<T1, T2, T3> {
public:
void F1( T1 t1, T2* t2, T3* t3 ) {
cout << "\nPartially Specialized Template Class - Function F1 invoked ..." << endl;
cout << "Value of t1 is " << t1 << endl;
cout << "Value of t2 is " << *t2 << endl;
cout << "Value of t3 is " << *t3 << endl;
}
};
- 앞의 코드에서 기본 템플릿 클래스 이름과 부분적으로 특수화된 클래스 이름이 전체 또는 명시적 템플릿 클래스 특수화의 경우와 동일하다는 것을 알 수 있습니다.
- 그러나 템플릿 매개 변수 표현식에는 일부 구문 변경 사항이 있습니다.
- 전체 템플릿 클래스 특수화의 경우 템플릿 매개변수 표현식이 비어 있는 반면 부분 특수화 템플릿 클래스의 경우 다음과 같이 나열됩니다.
template <typename T1, typename T2, typename T3>
class MyTemplateClass< T1, T2*, T3*> : public MyTemplateClass<T1, T2, T3> { };
- 템플릿<typename T1, typename T2, typename T3> 표현식은 기본 클래스 템플릿 클래스에서 사용되는 템플릿 매개변수 표현식이고 MyTemplateClass< T1, T2*, T3*>는 두 번째 클래스에서 수행하는 부분 특수화입니다.
- 보시다시피 두 번째 클래스는 두 번째 클래스에서 포인터로 사용되는 typename T2 및 typename T3에 대해 일부 특수화를 수행했습니다.
- 그러나 typename T1은 두 번째 클래스에서 그대로 사용됩니다.
- 지금까지 설명한 사실 외에도 두 번째 클래스는 기본 템플릿 클래스를 상속하므로 두 번째 클래스가 기본 템플릿 클래스의 공용 및 보호 메서드를 재사용하는 데 도움이 됩니다.
- 그러나 부분 템플릿 특수화는 특수화된 클래스가 다른 기능을 지원하는 것을 중지하지 않습니다.
- 기본 템플릿 클래스의 F1 기능은 부분적으로 특수화된 템플릿 클래스로 대체되지만 상속을 통해 기본 템플릿 클래스의 F2 기능을 재사용합니다.
#include "partiallyspecialized.h"
int main () {
int x = 10;
int *y = &x;
int *z = &x;
MyTemplateClass<int, int*, int*> obj;
obj.F1(x, y, z);
obj.F2(x, x);
return 0;
}
- 결과는 아래와 같습니다.
Partially Specialized Template Classs - Function F1 invoked ...
Value of t1 is 10
Value of t2 is 10
Value of t3 is 10
Primary Tempalte Class - Function F2 invoked ...
Value of t1 is 10
Value of t2 is 20
728x90
'CS > C++' 카테고리의 다른 글
[Mastering C++ Programming] - Issues with raw pointers (0) | 2022.12.07 |
---|---|
[Mastering C++ Programming] - Memory Management (0) | 2022.12.07 |
[Mastering C++ Programming] - Explicit class specializations (0) | 2022.12.06 |
[Mastering C++ Programming] - Class template (1) | 2022.12.06 |
[Mastering C++ Programming] - Overloading function templates (0) | 2022.12.06 |
Comments