ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::scope_guard< F > Class Template Reference

RAII作用域守卫类。 More...

#include <scope_guard.h>

Public Member Functions

template<typename Func>
requires (!std::same_as<std::remove_cvref_t<Func>, scope_guard<F>>)
 scope_guard (Func &&fun) noexcept
 构造函数。
 ~scope_guard () noexcept
 析构函数,执行回调函数。

Detailed Description

template<typename F>
requires std::invocable<F>
class ngl::scope_guard< F >

RAII作用域守卫类。

析构时自动执行构造时传入的回调函数,保证异常安全。 支持C++17 CTAD(类模板参数推导)。

Template Parameters
F回调函数类型
Note
不可拷贝和移动。
析构时的异常会被静默捕获。
{
scope_guard guard([&]() { cleanup(); });
// ... 执行操作
} // guard析构时自动调用cleanup()

Constructor & Destructor Documentation

◆ scope_guard()

template<typename F>
template<typename Func>
requires (!std::same_as<std::remove_cvref_t<Func>, scope_guard<F>>)
ngl::scope_guard< F >::scope_guard ( Func && fun)
inlineexplicitnoexcept

构造函数。

Template Parameters
Func回调函数类型
Parameters
fun析构时执行的回调函数

◆ ~scope_guard()

template<typename F>
ngl::scope_guard< F >::~scope_guard ( )
inlinenoexcept

析构函数,执行回调函数。

回调函数抛出的异常会被静默捕获。


The documentation for this class was generated from the following file: