File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments