1 | #include <algorithm> |
2 | #include <functional> |
3 | #include <map> |
4 | #include <ranges> |
5 | #include <vector> |
6 | |
7 | bool sort_less(int a, int b) { |
8 | __builtin_printf("break here" ); |
9 | return a < b; |
10 | } |
11 | |
12 | bool ranges_sort_less(int a, int b) { |
13 | __builtin_printf("break here" ); |
14 | return a < b; |
15 | } |
16 | |
17 | int view_transform(int a) { |
18 | __builtin_printf("break here" ); |
19 | return a * a; |
20 | } |
21 | |
22 | void test_algorithms() { |
23 | std::vector<int> vec{8, 1, 3, 2}; |
24 | |
25 | // The internal frames for `std::sort` should be hidden |
26 | std::sort(first: vec.begin(), last: vec.end(), comp: sort_less); |
27 | |
28 | // The internal frames for `ranges::sort` should be hidden |
29 | std::ranges::sort(vec.begin(), vec.end(), ranges_sort_less); |
30 | |
31 | // Same for views |
32 | for (auto x : vec | std::ranges::views::transform(view_transform)) { |
33 | // no-op |
34 | } |
35 | } |
36 | |
37 | void consume_number(int i) { __builtin_printf("break here" ); } |
38 | |
39 | int invoke_add(int i, int j) { |
40 | __builtin_printf("break here" ); |
41 | return i + j; |
42 | } |
43 | |
44 | struct Callable { |
45 | Callable(int num) : num_(num) {} |
46 | void operator()(int i) const { __builtin_printf("break here" ); } |
47 | void member_function(int i) const { __builtin_printf("break here" ); } |
48 | int num_; |
49 | }; |
50 | |
51 | void test_invoke() { |
52 | // Invoke a void-returning function |
53 | std::invoke(fn&: consume_number, args: -9); |
54 | |
55 | // Invoke a non-void-returning function |
56 | std::invoke(fn&: invoke_add, args: 1, args: 10); |
57 | |
58 | // Invoke a member function |
59 | const Callable foo(314159); |
60 | std::invoke(fn: &Callable::member_function, args: foo, args: 1); |
61 | |
62 | // Invoke a function object |
63 | std::invoke(fn: Callable(12), args: 18); |
64 | } |
65 | |
66 | struct MyKey { |
67 | int x; |
68 | bool operator==(const MyKey &) const = default; |
69 | bool operator<(const MyKey &other) const { |
70 | __builtin_printf("break here" ); |
71 | return x < other.x; |
72 | } |
73 | }; |
74 | |
75 | void test_containers() { |
76 | std::map<MyKey, int> map; |
77 | map.emplace(args: MyKey{.x: 1}, args: 2); |
78 | map.emplace(args: MyKey{.x: 2}, args: 3); |
79 | } |
80 | |
81 | int main() { |
82 | test_algorithms(); |
83 | test_invoke(); |
84 | test_containers(); |
85 | return 0; |
86 | } |
87 | |