Skip to content

Commit 105feda

Browse files
committed
Added example of chaining of function call
1 parent 551cd03 commit 105feda

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
f2 := fopen("variable2.txt", "w").on_scope_exit(fclose);
12+
f2.get().fprintf("you can handle smart_ptrs without changing behaviour of UFCS");
13+
14+
m := fopen("manual.txt", "w");
15+
m.fprintf("Manual handling still works");
16+
m.fclose();
17+
18+
/*
19+
// When UFCS is modified to extract pointers from smart pointers (when other matches failed)
20+
// then below code will work - separate PR prepared
21+
22+
f := fopen("variable.txt", "w").on_scope_exit(fclose);
23+
f.fprintf("This one use variable and\n");
24+
f.fprintf(" separate\n");
25+
f.fprintf(" calls\n");
26+
f.fprintf(" of fprintfs");
27+
28+
fopen("one_liner.txt", "w").on_scope_exit(fclose)
29+
.fprintf("This is oneliner!!\n\nAnd it just close automatically");
30+
*/
31+
32+
}

0 commit comments

Comments
 (0)