forked from furkankirac/cs409-2023-24-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week13-app4.cpp
71 lines (54 loc) · 1.44 KB
/
week13-app4.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
// lambdas capturing local variables: this includes local parameter packs as well
// storing parameter packs: an elegant storage container; get_first, get_size, get_tail impl.
#include <iostream>
#include <vector>
using namespace std;
auto list(const auto& ... items) {
return [&items...](auto accessor) {
return accessor(items...);
};
};
auto get_first() {
return [](const auto& first, auto ...) {
return first;
};
}
auto get_rest() {
return [](const auto&, const auto& ... rest) {
return list(rest...);
};
}
auto get_last() {
return [](const auto& ... all) { // all is a parameter pack 100|1|'a'|"abc"|3.14
return (all, ...);
};
}
auto foo() {
auto a = 100;
auto b = 1;
auto c = 'a';
auto d = "abc";
auto e = 3.14;
auto l = list(a, b, c, d, e);
return l;
}
int main() {
auto v = vector{1, 2, 3, 4};
auto z = 'z';
auto l = list(100, 1, 'a', "abc", 3.14);
// l is a list instance returned by foo using references to objects that got destructed
// this will cause issues without any warning
// auto l = foo();
auto a = l(get_first());
cout << a << endl;
auto l_rest = l(get_rest());
auto b = l_rest(get_first());
cout << b << endl;
auto c = l(get_last());
cout << c << endl;
// int k = 10;
// auto lambda = [&k](int increment) { k += increment; };
// lambda(100);
// cout << k << endl;
return 0;
}