25template <
typename Callback>
26class FastScopeGuard final {
28 static_assert(std::is_nothrow_move_constructible_v<Callback>);
31 std::is_nothrow_invocable_v<Callback&&>,
32 "If the functions called in the body of the lambda are all 'noexcept', "
33 "please mark the lambda itself as 'noexcept'. If however, the contents "
34 "are not 'noexcept', use 'ScopeGuard' instead of 'FastScopeGuard'."
37 static_assert(std::is_void_v<std::invoke_result_t<Callback&&>>,
"Return type of Callback function should be void");
39 static_assert(std::is_nothrow_destructible_v<Callback>,
"Callback function destructor should be noexcept");
41 constexpr explicit FastScopeGuard(Callback callback)
noexcept : callback_(std::move(callback)) {}
43 constexpr FastScopeGuard(FastScopeGuard&& other)
noexcept
44 : callback_(std::move(other.callback_)), is_active_(std::exchange(other.is_active_,
false)) {}
46 constexpr FastScopeGuard& operator=(FastScopeGuard&& other)
noexcept {
48 callback_ = std::move(other.callback_);
49 is_active_ = std::exchange(other.is_active_,
false);
55 if (is_active_) std::move(callback_)();
58 constexpr void Release()
noexcept { is_active_ =
false; }
64 bool is_active_{
true};