Skip to content

Commit e75657f

Browse files
committed
Added example of chaining of function call
1 parent b3f3f70 commit e75657f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <vector>
2+
#include <memory>
3+
4+
template <typename T, typename D>
5+
struct scope_exit {
6+
scope_exit(T v, D d)
7+
: value(v)
8+
, deleter(d)
9+
{
10+
}
11+
12+
~scope_exit() {
13+
deleter(value);
14+
}
15+
16+
operator T&() { return value; }
17+
18+
scope_exit(const scope_exit&) = delete;
19+
scope_exit& operator=(const scope_exit&) = delete;
20+
scope_exit(scope_exit&& rhs) noexcept : value(std::move(rhs.value)), deleter(std::move(rhs.deleter)) {}
21+
scope_exit& operator=(scope_exit&& rhs) noexcept {
22+
value = std::move(rhs.value);
23+
deleter = std::move(rhs.deleter);
24+
return *this;
25+
}
26+
27+
private:
28+
T value;
29+
D deleter;
30+
};
31+
32+
template <typename T, typename D>
33+
auto on_scope_exit(T&& v, D&& d) {
34+
return scope_exit(std::forward<T>(v),std::forward<D>(d));
35+
}
36+
37+
main: () -> int = {
38+
39+
fopen("variable2.txt", "w").on_scope_exit(:(e:_) = {
40+
e.fprintf("you can handle smart_ptrs without changing behaviour of UFCS");
41+
e.fclose();
42+
});
43+
44+
m := fopen("manual.txt", "w");
45+
m.fprintf("Manual handling still works");
46+
m.fclose();
47+
48+
}

0 commit comments

Comments
 (0)