-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
189 lines (141 loc) · 6.04 KB
/
main.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <typeinfo>
#include <type_traits>
#if defined(_MSC_VER)
//https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019
#define FUNCTION_WITH_SIGNATURE __FUNCSIG__
#else
// https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
#define FUNCTION_WITH_SIGNATURE __PRETTY_FUNCTION__
#endif
#define PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T \
std::cout << "\"" << FUNCTION_WITH_SIGNATURE << "\", type name of T: " << typeid(T).name() << std::endl;
/**** Case 1: ParamType is a reference or a pointer, but not a universal reference. */
template<typename T>
void func_param_type_reference(T&){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
template<typename T>
void func_param_type_const_reference(const T&){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
template<typename T>
void func_param_type_pointer(T*){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
template<typename T>
void func_param_type_pointer_to_const(const T*){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
/**** Case 1: ParamType is a reference or a pointer, but not a universal reference. */
/**** Case 2: ParamType is a universal reference. */
template<typename T>
void func_param_type_universal_reference(T&&){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
/**** Case 2: ParamType is a universal reference. */
/**** Case 3: ParamType is neither a pointer nor a reference, i.e. pass by value. */
template<typename T>
void func_param_type_by_value(T){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
template<typename T>
void func_param_type_by_const_value(const T){
PRINT_FUNC_WITH_SIGNATURE_AND_TYPE_NAME_OF_T;
}
/**** Case 3: ParamType is neither a pointer nor a reference, i.e. pass by value. */
void case1(){
/**** Case 1: ParamType is a reference or a pointer, but not a universal reference.
*/
std::cout << "Case 1: ParamType is a reference or a pointer, but not a universal reference." << std::endl;
int x = 100;
const int x1 = x;
int& x2 = x;
const int& x3 = x;
func_param_type_reference(x); // T is int, T& is int&
func_param_type_reference(x1); // T is const int, T& is const int&
func_param_type_reference(x2); // T is int, T& is int&
func_param_type_reference(x3); // T is const int, T& is const int&
func_param_type_const_reference(x); // T is int, const T& is const int&
func_param_type_const_reference(x1); // T is int, const T& is const int&
func_param_type_const_reference(x2); // T is int, const T& is const int&
func_param_type_const_reference(x3); // T is int, const T& is const int&
int* y1 = &x;
const int* y2 = &x;
const int* const y3 = &x;
func_param_type_pointer(y1); // T is int, T* is int*
func_param_type_pointer(y2); // T is const int, T* is const int*
func_param_type_pointer(y3); // T is const int, T* is const int*
func_param_type_pointer_to_const(y1); // T is int, const T* is const int*
func_param_type_pointer_to_const(y2); // T is int, const T* is const int*
func_param_type_pointer_to_const(y3); // T is int, const T* is const int*
}
void case2(){
/**** Case 2: ParamType is a universal reference.
*/
std::cout << "Case 2: ParamType is a universal reference." << std::endl;
int x = 100;
const int x1 = x;
int& x2 = x;
const int& x3 = x;
func_param_type_universal_reference(x); // T is int&, T&& is int&
func_param_type_universal_reference(x1); // T is const int&, T&& is const int&
func_param_type_universal_reference(x2); // T is int&, T&& is int&
func_param_type_universal_reference(x3); // T is const int&, T&& is const int&
func_param_type_universal_reference(std::move(x)); // T is int, T&& is int&&
func_param_type_universal_reference(std::move(x1)); // T is const int, T&& is const int&&
func_param_type_universal_reference(100); // T is int, T&& is int&&
}
void case3(){
/**** Case 3: ParamType is neither a pointer nor a reference, i.e. pass by value.
*/
std::cout << "Case 3: ParamType is neither a pointer nor a reference, i.e. pass by value." << std::endl;
int x = 100;
const int x1 = x;
const int& x2 = x;
const int&& x3 = std::move(x);
int* y = &x;
const int* y1 = y;
const int* const y2 = y;
func_param_type_by_value(x); // T is int
func_param_type_by_value(x1); // T is int
func_param_type_by_value(x2); // T is int
func_param_type_by_value(x3); // T is int
func_param_type_by_value(y); // T is int*
func_param_type_by_value(y1); // T is const int*
func_param_type_by_value(y2); // T is const int*
func_param_type_by_const_value(x); // T is const int
func_param_type_by_const_value(x1); // T is const int
func_param_type_by_const_value(x2); // T is const int
func_param_type_by_const_value(x3); // T is const int
func_param_type_by_const_value(y); // T is int* const
func_param_type_by_const_value(y1); // T is const int* const
func_param_type_by_const_value(y2); // T is const int* const
}
void case_array_arguments(){
std::cout << "Case Array Arguments." << std::endl;
const char name[] = "J. P. Briggs";
func_param_type_by_value(name); // T is const char*
func_param_type_pointer(name); // T is const char, T* is const char*
func_param_type_reference(name);// T is const char [13], T& is const char(&)[13]
}
void some_func(int, double){}
void case_function_arguments(){
std::cout << "Case Function Arguments." << std::endl;
func_param_type_by_value(some_func); // T is void(*)(int, double)
func_param_type_pointer(some_func); // T is void(int, double), T* is void(*)(int, double)
func_param_type_reference(some_func); // T is void(int, double), T& is void(&)(int, double)
}
int main() {
case1();
std::cout << std::endl;
case2();
std::cout << std::endl;
case3();
std::cout << std::endl;
case_array_arguments();
std::cout << std::endl;
case_function_arguments();
std::cout << std::endl;
return 0;
}