-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke_intseq_exmpl.cc
177 lines (142 loc) · 6.57 KB
/
invoke_intseq_exmpl.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
#include "invoke_intseq.h"
#include <cstddef>
#include <algorithm>
#include <array>
#include <iostream>
#include <ranges>
#include <tuple>
#include <utility>
namespace
{
template <class... Args>
void Print(const Args&... args)
{
((std::cout << args << ' '), ...);
std::cout << std::endl;
}
template <class T, T... t>
struct Printer
{
static void Print()
{
((std::cout << t << ' '), ...);
std::cout << std::endl;
}
};
struct Foo
{
static size_t instances_count;
int id;
void Init()
{
id = instances_count++;
}
Foo()
{
Init();
std::cout << "Foo(): id = " << id << std::endl;
}
Foo(const Foo& o)
{
Init();
std::cout << "Foo(const Foo&) id = " << o.id << " -> id = " << id << std::endl;
}
Foo(Foo&& o)
{
id = o.id;
std::cout << "Foo(Foo&&) id = " << id << std::endl;
}
void Print()
{
std::cout << "Foo::Print() id = " << id << std::endl;
}
};
size_t Foo::instances_count = 0;
struct Caller
{
Foo foo;
template <class... Args>
void operator()(const Args&... args) const
{
Print(args...);
}
};
std::ostream& operator<<(std::ostream& s, const Foo& a)
{
s << "[Foo: " << a.id << "]";
return s;
}
} // koniec anonimowej przestrzeni nazw
int main()
{
Foo foo;
constexpr auto make_number = [](auto x, auto y, auto z) {return 100 * x + 10 * y + z;};
size_t s;
// Sprawdzenie typów wyników wywołań
static_assert(std::is_same_v<decltype(invoke_intseq([](auto...) {}, 1, 2, 3)), void>);
static_assert(std::is_same_v<decltype(invoke_intseq([](auto...) {return 0;}, 1, 2, 3)), int>);
static_assert(std::is_same_v<decltype(invoke_intseq([](auto...) {}, std::integer_sequence<int, 1, 2, 3>(), 4, 5)), void>);
static_assert(std::ranges::range<decltype(invoke_intseq([](auto...) {return 0;}, std::integer_sequence<int, 1, 2, 3>(), 4, 5))>);
static_assert(std::is_same_v<std::ranges::range_value_t<decltype(invoke_intseq([](auto...) {return 0;}, std::integer_sequence<int, 1, 2, 3>(), 4, 5))>, int>);
std::cout << "empty" << std::endl;
invoke_intseq([]() {std::cout << "nothing" << std::endl;});
std::cout << "single with lambda" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, 1, 2, 3);
std::cout << "single with function pointer" << std::endl;
invoke_intseq(Print<int, int, int>, 4, 5, 6);
invoke_intseq(&Foo::Print, foo);
std::cout << "size_t" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, std::integer_sequence<int, 1, 2, 3>(), size_t(10), std::integer_sequence<int, 7, 8>());
std::cout << "std::integral_constant, size_t" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, std::integer_sequence<int, 1, 2, 3>(), std::integral_constant<size_t, 11>(), std::integer_sequence<int, 7, 8>());
std::cout << "string" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, "ala", size_t(10), std::integer_sequence<int, 7, 8>());
std::cout << "template Print single" << std::endl;
auto template_print = [=](auto... a) {Printer<int, make_number(a...)>::Print();};
invoke_intseq(template_print, std::integer_sequence<int, 1, 2>(), std::integral_constant<int, 3>(), std::integer_sequence<int, 7, 8>());
std::cout << "template Print all" << std::endl;
invoke_intseq([](auto... a) {Printer<int, a...>::Print();}, std::integer_sequence<int, 1, 2>(), std::integral_constant<int, 3>(), std::integer_sequence<int, 7, 8>());
std::cout << "single result" << std::endl;
std::cout << invoke_intseq(make_number, 9, 8, 7) << std::endl;
static_assert(invoke_intseq(make_number, 9, 8, 7) == 987);
std::cout << "integer sequence single result" << std::endl;
for (auto i : invoke_intseq(make_number, std::integer_sequence<int, 6>(), 5, 4))
std::cout << i << std::endl;
static_assert(std::ranges::equal(invoke_intseq(make_number, std::integer_sequence<int, 6>(), 5, 4), std::array{654}));
std::cout << "integer sequence empty result" << std::endl;
for (auto i : invoke_intseq(make_number, std::integer_sequence<int>(), 5, 4))
std::cout << i << std::endl;
static_assert(std::ranges::equal(invoke_intseq(make_number, std::integer_sequence<int>(), 5, 4), std::array<int, 0>{}));
std::cout << "integer sequence multiple result" << std::endl;
for (auto i : invoke_intseq(make_number, 1, std::integer_sequence<int, 2, 3>(), std::integer_sequence<int, 4, 5>()))
std::cout << i << std::endl;
static_assert(std::ranges::equal(invoke_intseq(make_number, 1, std::integer_sequence<int, 2, 3>(), std::integer_sequence<int, 4, 5>()), std::array{124, 125, 134, 135}));
std::cout << "tuple result" << std::endl;
for (const auto& t : invoke_intseq([](auto a, auto b, int c, auto d) {return std::make_tuple(a, b, c, d);}, "ala", "ma", std::integer_sequence<int, 5, 7, 9>(), "kotów"))
std::apply([](auto... a) {Print(a...);}, t);
std::cout << "Foo: const auto&" << std::endl;
invoke_intseq([](const auto&... a) {Print(a...);}, Foo(), foo);
std::cout << "Foo: auto" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, Foo(), foo);
std::cout << "Foo: auto&" << std::endl;
// Nie powinno się kompilować: rvalue nie może być przekazywane jako auto&.
// invoke_intseq([](auto&... a) {Print(a...);}, Foo(), foo, std::make_index_sequence<3>());
invoke_intseq([](auto&... a) {Print(a...);}, foo, foo);
std::cout << "Foo: const auto& integer_sequence" << std::endl;
invoke_intseq([](const auto&... a) {Print(a...);}, std::make_index_sequence<2>(), Foo(), foo);
std::cout << "Foo: auto integer_sequence" << std::endl;
invoke_intseq([](auto... a) {Print(a...);}, std::make_index_sequence<2>(), Foo(), foo);
std::cout << "reference passing with integer_sequence" << std::endl;
s = 0;
auto ref_seq = invoke_intseq([](size_t a, size_t& r) -> size_t& {r += a; return r;}, std::make_index_sequence<5>(), s);
std::cout << s << std::endl;
for (size_t& r : ref_seq)
r++;
std::cout << s << std::endl;
std::cout << "reference result single" << std::endl;
invoke_intseq([](size_t a, size_t& r) -> size_t& {r += a; return r;}, 200, s) += 3000;
std::cout << s << std::endl;
std::cout << "Caller" << std::endl;
Caller caller;
invoke_intseq(caller, std::integer_sequence<int, 3, 2, 1>(), "caller");
}