Skip to content

Commit cea865b

Browse files
committed
Added example of chaining of function call
correct the test
1 parent 1b1fc9b commit cea865b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <vector>
2+
#include <memory>
3+
4+
template <typename T, typename R>
5+
auto on_scope_exit(T* f, R(*close)(T*)) {
6+
return std::unique_ptr<T, decltype(close)>{f, close};
7+
}
8+
9+
main: () -> int = {
10+
11+
fopen("variable2.txt", "w").on_scope_exit(:(e:_) {
12+
e.fprintf("you can handle smart_ptrs without changing behaviour of UFCS");
13+
e.fclose();
14+
});
15+
16+
m := fopen("manual.txt", "w");
17+
m.fprintf("Manual handling still works");
18+
m.fclose();
19+
20+
/*
21+
// When UFCS is modified to extract pointers from smart pointers (when other matches failed)
22+
// then below code will work - separate PR prepared
23+
24+
f := fopen("variable.txt", "w").on_scope_exit(fclose);
25+
f.fprintf("This one use variable and\n");
26+
f.fprintf(" separate\n");
27+
f.fprintf(" calls\n");
28+
f.fprintf(" of fprintfs");
29+
30+
fopen("one_liner.txt", "w").on_scope_exit(fclose)
31+
.fprintf("This is oneliner!!\n\nAnd it just close automatically");
32+
*/
33+
34+
}

0 commit comments

Comments
 (0)