-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path305.md
211 lines (169 loc) · 5.16 KB
/
305.md
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
<details open><summary>Info</summary><p>
* **Did you know about (rejected) proposal for homogeneous variadic function parameters?**
* https://wg21.link/P1219 (https://github.com/cplusplus/papers/issues/297)
</p></details><details open><summary>Example</summary><p>
```cpp
#include <concepts>
// auto fn(int...) -> void; // error
auto fn(int, ...) -> void ; // ok
auto fn(std::same_as<int> auto...) -> void; // ok
```
> https://godbolt.org/z/cfq78aWj5
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `safe_call` which will call given function with variadic type-safe input parameters from propagted from ...?**
* Max number of parameters is 3
* fn is expected to be called with types coresponding to input parameters (not just strings, for example)
```cpp
auto safe_call(auto fn, auto fmt, ...); // TODO
int main() {
{
std::stringstream str{};
safe_call([&](auto... args) { ((str << args), ...); }, "");
assert(std::string{""} == str.str());
}
{
std::stringstream str{};
safe_call([&](auto... args) { ((str << args), ...); }, "id", 4, 2.);
assert(std::string{"42"} == str.str());
}
{
std::stringstream str{};
safe_call([&](auto... args) { ((str << args), ...); }, "di", 3.2, 1);
assert(std::string{"3.21"} == str.str());
}
{
std::stringstream str{};
safe_call([&](auto... args) { ((str << args), ...); }, "idi", 1, 2.3, 4);
assert(std::string{"12.34"} == str.str());
}
}
```
> https://godbolt.org/z/eT7vnM13E
</p></details><details><summary>Solutions</summary><p>
```cpp
void assert_fmt(char fmt, char required_fmt) {
if (fmt != required_fmt) {
throw std::runtime_error(std::string("fmt must be '") + required_fmt + "'");
}
}
template<class T>
requires std::same_as<T, int>
void assert_fmt(char fmt) {
assert_fmt(fmt, 'i');
}
template<class T>
requires std::same_as<T, double>
void assert_fmt(char fmt) {
assert_fmt(fmt, 'd');
}
template<class T>
auto safe_call_helper(auto fn, auto fmt, std::same_as<T> auto arg) {
assert_fmt<T>(*fmt);
fn(arg);
}
template<class T, class U>
auto safe_call_helper(auto fn, auto fmt, T arg_0, U arg_1, auto ... args) {
assert_fmt<T>(*fmt);
fn(arg_0);
safe_call_helper<U>(fn, fmt + 1, arg_1, args...);
}
template<class ... Ts>
using FirstType = std::tuple_element_t<0, std::tuple<Ts...>>;
template<class ... Ts>
auto safe_call(auto fn, auto fmt, Ts ... args) {
if constexpr (sizeof...(Ts)) {
safe_call_helper<FirstType<Ts...>>(fn, fmt, args...);
}
```
> https://godbolt.org/z/bhPxaf7Ga
```cpp
template <auto N>
auto safe_call_impl(auto fn, auto fmt, va_list args, auto... ts) {
if constexpr (N == 0) {
fn(ts...);
} else {
if (*fmt) {
switch (*fmt) {
case 'i':
return safe_call_impl<N - 1>(fn, fmt + 1, args, ts...,
va_arg(args, int));
case 'd':
return safe_call_impl<N - 1>(fn, fmt + 1, args, ts...,
va_arg(args, double));
}
} else {
fn(ts...);
}
}
}
auto safe_call(auto fn, auto fmt, ...) {
std::va_list args{};
va_start(args, fmt);
safe_call_impl<3>(fn, fmt, args);
va_end(args);
```
> https://godbolt.org/z/8bs8qe4MG
```cpp
auto safe_call(auto fn, std::string_view fmt, ...) -> decltype(auto) {
std::va_list args;
va_start(args, fmt);
using namespace boost::mp11;
return mp_with_index<4>(
std::size(fmt), [&]<auto N>(mp_size_t<N>) -> decltype(auto) {
mp_repeat_c<std::tuple<std::variant<int, double>>, N> t;
mp_for_each<mp_iota_c<N>>([&]<auto I>(mp_size_t<I>) {
switch (fmt[I]) {
case 'i':
std::get<I>(t) = va_arg(args, int);
break;
case 'd':
std::get<I>(t) = va_arg(args, double);
break;
}
});
va_end(args);
return std::apply(
[&](const auto &...vars) -> decltype(auto) {
return std::visit(fn, vars...);
},
t);
});
}
```
> https://godbolt.org/z/bvWq9vEna
```cpp
auto safe_call(auto fn, auto fmt, ...) {
std::va_list args;
va_start(args, fmt);
std::stringstream sout;
while(*fmt != '\0') {
if (*fmt=='d')
fn(va_arg(args, double));
if (*fmt =='i')
fn(va_arg(args, int));
fmt++;
}
va_end(args);
}
```
> https://godbolt.org/z/TbYrEcPfG
```cpp
auto safe_call(auto fn, auto fmt, auto... args){
auto safe_check = [&]<std::size_t... Ns>(std::index_sequence<Ns...>){
auto type_check = [](char type, auto arg){
if( type == 'i'){
return std::is_convertible_v<decltype(arg),int>;
}else if( type == 'd'){
return std::is_convertible_v<decltype(arg),float>;
}
return false;
};
return (true && ... && type_check(fmt[Ns],args));
}(std::make_index_sequence<sizeof...(args)>{});
if(safe_check)
{
return fn(args...);
}
}
```
> https://godbolt.org/z/5foWccqee