-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
function_ptr_typedef_vs_std_function.cpp
217 lines (172 loc) · 4.86 KB
/
function_ptr_typedef_vs_std_function.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
4 Feb. 2022
Passing a function ptr as a parameter or argument to another function!:
Remind myself how to use C-style function pointers, vs `typdedef`ed function pointers, vs C++
`std::function<>` objects.
STATUS: EXCELLENT! IT WORKS!
To compile and run (assuming you've already `cd`ed into this dir):
1. In C++
```bash
g++ -Wall -Wextra -Werror -O3 -std=c++17 function_ptr_typedef_vs_std_function.cpp -o bin/a && bin/a
```
References:
1. `std::function<>`: https://en.cppreference.com/w/cpp/utility/functional/function
1. *****+ How to typedef a function pointer in C or C++: https://stackoverflow.com/q/4295432/4561887
1. ***** [my question] https://stackoverflow.com/questions/31869026/c-function-call-via-an-object-with-public-member-pointer-to-function-without
*/
// C++ includes
#include <functional> // `std::function<>`
#include <iostream> // For `std::cin`, `std::cout`, `std::endl`, etc.
// C includes
#include <cstdint> // For `uint8_t`, `int8_t`, etc.
#include <cstdio> // For `printf()`
typedef void (*voidvoidfunc_t)(void);
using voidvoidfunc2_t = std::function<void(void)>;
// -------------------------------------------------------------------------------
// BASIC `void(void)` PRINT FUNCTIONS TO BE PASSED AS ARGUMENTS TO OTHER FUNCTIONS
// -------------------------------------------------------------------------------
void print1()
{
printf("print1\n");
}
void print2()
{
printf("print2\n");
}
void print3()
{
printf("print3\n");
}
// -------------------------------------------------------------------------------
// FUNCTIONS WHICH ACCEPT OTHER `void(void)` FUNCTIONS
// -------------------------------------------------------------------------------
// -------------------
// C-STYLE TECHNIQUES:
// -------------------
// [commonly-used in C, but hard to read] Using a raw function pointer
void printWithFunc1(void (*f)(void))
{
f();
}
// [my favorite usage in C!] Using the typedef `voidvoidfunc_t` to a fuction pointer
// (much easier to read than above!)
void printWithFunc2(voidvoidfunc_t f)
{
f();
}
// -------------------
// C++ TECHNIQUES:
// -------------------
// [easy-to-read in C++] Using a `std::function<>` directly
void printWithFunc3(std::function<void(void)> f)
{
f();
}
// [very similar to the C typedef usage above] Using the `using`-style "typdedef" to a
// `std::function<>`
void printWithFunc4(voidvoidfunc2_t f)
{
f();
}
// -------------------
// fancy function **call* practice; see my Q here: https://stackoverflow.com/q/31869026/4561887
// -------------------
void printWithFunc5(voidvoidfunc_t f)
{
// see my Q and the answers here!: https://stackoverflow.com/q/31869026/4561887
f();
(*f)();
(**f)();
(***f)();
// etc.
(**********************************f)();
printf("- - -\n");
}
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
printf("Hello ");
std::cout << "world!\n\n";
printf("printWithFunc1\n");
printWithFunc1(print1);
printWithFunc1(print2);
printWithFunc1(print3);
printf("\n");
printf("printWithFunc2\n");
printWithFunc2(print1);
printWithFunc2(print2);
printWithFunc2(print3);
printf("\n");
printf("printWithFunc3\n");
printWithFunc3(print1);
printWithFunc3(print2);
printWithFunc3(print3);
printf("\n");
printf("printWithFunc4\n");
printWithFunc4(print1);
printWithFunc4(print2);
printWithFunc4(print3);
printf("\n");
printf("---------------\n"
"Let's do a few more tests for kicks.\n"
"Notice how passing the **address** to a function makes no difference.\n"
"See my Q here: https://stackoverflow.com/q/31869026/4561887\n");
printWithFunc1(&print1);
printWithFunc1(&print2);
printWithFunc1(&print3);
printf("\n");
printf("Fancy function calls:\n");
printWithFunc5(print1);
printWithFunc5(&print2); // intentional & in front--makes no difference
printWithFunc5(print3);
return 0;
}
/*
SAMPLE OUTPUT:
eRCaGuy_hello_world/cpp$ g++ -Wall -Wextra -Werror -O3 -std=c++17 function_ptr_typedef_vs_std_function.cpp -o bin/a && bin/a
Hello world!
printWithFunc1
print1
print2
print3
printWithFunc2
print1
print2
print3
printWithFunc3
print1
print2
print3
printWithFunc4
print1
print2
print3
---------------
Let's do a few more tests for kicks.
Notice how passing the **address** to a function makes no difference.
See my Q here: https://stackoverflow.com/q/31869026/4561887
print1
print2
print3
Fancy function calls:
print1
print1
print1
print1
print1
- - -
print2
print2
print2
print2
print2
- - -
print3
print3
print3
print3
print3
- - -
*/