ohmu
·
2026-10-15
1#pragma once
2
3#include <utility>
4#include <type_traits>
5
6template <typename Fn>
7struct on_scope_exit {
8 static_assert(std::is_nothrow_move_constructible<Fn>::value,
9 "Fn must be nothrow move constructible");
10
11 explicit on_scope_exit(Fn&& fn) noexcept
12 : _fn(std::move(fn)) {};
13 on_scope_exit(const on_scope_exit&) = delete;
14 on_scope_exit& operator=(const on_scope_exit&) = delete;
15 ~on_scope_exit() noexcept {
16 this->_fn();
17 }
18
19 private:
20 Fn _fn;
21};