kaycxx-core
C++ core library
Loading...
Searching...
No Matches
scope.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
11#include <concepts>
12#include <type_traits>
13#include <utility>
14
15namespace kaycxx::core {
16
23template <typename F>
24requires std::invocable<F&> && std::is_nothrow_invocable_v<F&>
25class [[nodiscard]] scope_exit {
26public:
32 explicit scope_exit(F callback) : callback_{std::move(callback)} {}
33 ~scope_exit() noexcept {
34 callback_();
35 }
36 scope_exit(const scope_exit&) = delete;
37 scope_exit& operator=(const scope_exit&) = delete;
38 scope_exit(scope_exit&&) = delete;
39 scope_exit& operator=(scope_exit&&) = delete;
40
41private:
42 F callback_;
43};
44
46template <typename F>
48
49} // namespace kaycxx::core
Executes a callback when the current scope exits.
Definition scope.hpp:25
scope_exit(F callback)
Creates a scope guard that runs the callback during destruction.
Definition scope.hpp:32
Core C++ functionality.
Definition core.hpp:15