-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathp0.cpp
executable file
·50 lines (37 loc) · 1.22 KB
/
p0.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
// Copyright (c) 2015 Vittorio Romeo
// License: AFL 3.0 | https://opensource.org/licenses/AFL-3.0
// http://vittorioromeo.info | [email protected]
#include <iostream>
#include <initializer_list>
// Welcome to my talk:
// "`for_each_argument` explained and expanded".
// During this presentation we'll take a look at a very interesting
// code snippet, originally posted on Twitter by Sean Parent.
// {image: p0_d0}
// We'll also cover a very useful C++14 standard library addition,
// "compile-time integer sequences", and briefly take a look at C++17
// "fold expressions".
// ------------------------------------------------------------------
// So, what does `for_each_argument` do?
// Well, the name is pretty self-explanatory...
// It invokes a callable object on every passed argument.
template <class F, class... Ts>
void for_each_argument(F f, Ts&&... a) {
(void)std::initializer_list<int>{(f(std::forward<Ts>(a)), 0)...};
}
int main()
{
// Prints "hello123".
for_each_argument
(
[](const auto& x){ std::cout << x; },
"hello",
1,
2u,
3.f
);
std::cout << "\n";
return 0;
}
// That is cool. How does it work?
// Let's discover that in the next code segment.