-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariadic_template.cpp
More file actions
140 lines (119 loc) · 2.79 KB
/
variadic_template.cpp
File metadata and controls
140 lines (119 loc) · 2.79 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
#include <iostream>
class Integer
{
int *m_pInt;
public:
// Default constructor
Integer();
// Parameterized constructor
Integer(int value);
// Copy constructor
Integer(const Integer &obj);
// Move constructor
Integer(Integer &&obj);
int GetValue() const;
void SetValue(int value);
~Integer();
};
Integer::Integer()
{
std::cout << "Integer()" << std::endl;
m_pInt = new int(0);
}
Integer::Integer(int value)
{
std::cout << "Integer(int)" << std::endl;
m_pInt = new int(value);
}
Integer::Integer(const Integer &obj)
{
std::cout << "Integer(const Integer&)" << std::endl;
m_pInt = new int(*obj.m_pInt);
}
Integer::Integer(Integer &&obj)
{
std::cout << "Integer(int&&)" << std::endl;
m_pInt = obj.m_pInt;
obj.m_pInt = nullptr;
}
int Integer::GetValue() const
{
return *m_pInt;
}
void Integer::SetValue(int value)
{
*m_pInt = value;
}
Integer::~Integer()
{
std::cout << "~Integer()" << std::endl;
delete m_pInt;
}
class Employee
{
std::string m_Name;
Integer m_Id;
public:
// Employee(const std::string &name, const Integer &id) :
// m_Name{ name },
// m_Id{ id } {
// std::cout << "Employee(const std::string &name, const Integer &id)" << std::endl;
// }
template <typename T1, typename T2>
Employee(T1 &&name, T2 &&id) : m_Name{std::forward<T1>(name)},
m_Id{std::forward<T2>(id)}
{
std::cout << "Employee(std::string &&name, Integer &&id)" << std::endl;
}
};
template <typename T1, typename T2>
Employee *Create(T1 &&a, T2 &&b)
{
return new Employee(std::forward<T1>(a), std::forward<T2>(b));
}
int main1()
{
// Employee emp1{ "Umar", Integer{100} };
/*std::string name = "Umar";
Employee emp2{ name, 100 };
Integer val{ 100 };
Employee emp3{ std::string{"Umar"}, val };*/
auto emp = Create("Umar", Integer{100});
return 0;
}
void Print()
{
std::cout << "Print() called " << std::endl;
}
std::ostream &operator<<(std::ostream &out, const Integer &a)
{
out << a.GetValue();
return out;
}
// Template parameter pack
template <typename T, typename... Params>
// Function parameter pack
void Print(T &&a, Params &&...args)
{
// std::cout << "sizeof...(args): " << sizeof...(args) << std::endl;
// std::cout << sizeof...(Params) << std::endl;
if (sizeof...(args) >= 0)
{
std::cout << a << ",";
}
// We can forward a function parameter pack
Print(std::forward<Params>(args)...);
}
int main()
{
Integer i1(0);
auto a = {1, 2, 3, 4}; // initializer list
std::cout << "main()\n";
Print(1, 2.5, 3, "4abc");
Integer val{100};
Print(0, val, Integer{200});
// Print(a); //initializer_list so fails
return 0;
}