-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartialTemplateSpecialization.cpp
67 lines (52 loc) · 1.16 KB
/
PartialTemplateSpecialization.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
template<typename T, int some_number>
class CustomType
{
private:
T m_value;
public:
CustomType() = default;
void print() { std::cout << "i am whatever i am" << std::endl; }
};
template<int num>
void print(CustomType<std::string, num> type)
{
std::cout << "i am a string and i have a number: " << num << std::endl;
}
//
//
//
// duplicate code
// template<int some_number>
// class CustomType<std::string, some_number>
// {
// private:
// std::string m_value;
// public:
// CustomType() = default;
// void print() { std::cout << "i am a string and i have a number: " << some_number << std::endl; }
// };
//
//
//
// Less duplicate code. way 2 go
template<typename T, int some_number>
class Derived : public CustomType<T, some_number> {};
template<int some_number>
class Derived<std::string, some_number> : public CustomType<std::string, some_number>
{
public:
void print()
{
std::cout << "I am a string and i have a number: " << some_number << std::endl;
}
};
int main()
{
CustomType<int, 123> i;
CustomType<std::string, 1> str;
i.print();
str.print();
print(str);
return 0;
}