我在哪里可以找到适合我的 C++ 的良好 Scope Guard 实现?项目?

发布于 2024-09-14 05:13:34 字数 115 浏览 13 评论 0原文

我最近刚刚了解了 Scope Guard C++ 习惯用法。不幸的是我找不到它的任何好的实现。

谁能给我指出一些良好且可用的 C++ Scope Guard 实现?

谢谢,博达·西多。

I just recently learned about Scope Guard C++ idiom. Unfortunately I can't find any good implementation of it.

Can anyone point me to some good and usable Scope Guard implementation in C++?

Thanks, Boda Cydo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

極樂鬼 2024-09-21 05:13:35

Folly 库(来自 facebook 的开源)也提供了一个实现(这并不奇怪,因为他们使用了 AA):

https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h

我认为这个和这里提到的 MNMLSTC 实现都值得考虑。

The Folly library (open-source from facebook) also provides an implementation (not surprising since A.A. is employed by them):

https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h

I think this and the MNMLSTC implementation mentioned here are both worth consideration.

離人涙 2024-09-21 05:13:35

让我提供一个基本的 C++20 版本。

#include <concepts>
#include <type_traits>

template <std::invocable Cleanup>
class [[nodiscard]] scope_guard
{
    Cleanup d;
public:
    scope_guard(Cleanup&& d) : d{std::forward<Cleanup>(d)} {}
    scope_guard(const scope_guard&) = delete;

    ~scope_guard(){d();}
};

// allow construction from plain function
template <typename F>
scope_guard(F&&) -> scope_guard<std::decay_t<F>>;

请注意,除非我们需要移动scope_guard,否则它会比Cleanup可调用项增加零内存开销,因为我们不需要保留它以可重置的方式,因为我们不需要移动构造函数,因为我们得到了类模板参数推导。

这是语言变得多么富有表现力的一个很好的例子。谢谢委员!

Let me offer a basic C++20 version.

#include <concepts>
#include <type_traits>

template <std::invocable Cleanup>
class [[nodiscard]] scope_guard
{
    Cleanup d;
public:
    scope_guard(Cleanup&& d) : d{std::forward<Cleanup>(d)} {}
    scope_guard(const scope_guard&) = delete;

    ~scope_guard(){d();}
};

// allow construction from plain function
template <typename F>
scope_guard(F&&) -> scope_guard<std::decay_t<F>>;

Please note that unless we need to move the scope_guard around, it adds zero memory overhead over Cleanup callable, because we don't need to hold it in a resettable manner, because we don't need move constructor, because we got class template argument deduction.

A fine example of how expressive the language became. Thank you commitee!

流年已逝 2024-09-21 05:13:35

“Scope Guard”对象只是更广泛的 RAII 习惯用法的一个实例。

并且没有单一的实现。这是 C++ 程序员必须理解的东西,而不仅仅是复制/粘贴。幸运的是,实施起来也非常简单。

您创建一个代表某种资源的类。当类被实例化(由其构造函数之一)时,它应该获取资源,如果失败则抛出异常。当类被销毁时,它应该处置资源,执行所有必要的清理。

就是这样。您还必须处理复制构造函数和赋值运算符(通过克隆资源或将这两个函数设为私有,以便永远不会调用它们)。

您不需要找到“一个好的实现”,因为您将自己编写数十个不同的实现。它们编写起来很简单,并且不容易重用,因为每个都包装不同类型的资源。

A "Scope Guard" object is just one instance of the much broader RAII idiom.

And there is no single implementation of that. It is something a C++ programmer has to understand, not just copy/paste. Luckily, it is also pretty trivial to implement.

You create a class which represents some kind of resource. When the class is instantiated (by one of its constructors), it should acquire the resource, and throw an exception if that fails. When the class is destroyed, it should dispose of the resource, performing all the necessary cleanup.

And... that's it. You also have to handle copy constructor and assignment operator (either by cloning the resource or by making these two functions private so they're never called).

You don't need to find "a good implementation", because you're going to write dozens and dozens of different implementations yourself. They're trivial to write, and they can't easily be reused because each one wraps a different type of resource.

最偏执的依靠 2024-09-21 05:13:35

有人建议将scope_guard 添加到标准库中。您可以阅读该论文,其中包括可以复制/粘贴的示例实现,这里。具体实现见9.1节。

There's a proposal to add scope_guard to the standard library. You can read the paper, which includes a sample implementation you can copy/paste, here. See section 9.1 for the implementation.

夜清冷一曲。 2024-09-21 05:13:34

原始 ScopeGuard 类包含在 Andrei Alexandrescu 和 Petru Marginean 撰写的这篇 Dobb 博士的文章中。 此处提供了略有改进的版本,其中由 Joshua Lehrer 进行了一些更改。 (Lehrer 的版本是我在项目中使用的版本。)它也包含在 Loki 库。

Boost 现在有一个 ScopeExit 库,它是比 ScopeGuard 更强大(因为它可以执行任意代码,而 ScopeGuard 只能调用单个预先存在的函数)。

编辑:综上所述,Scope Guard 实际上只是 RAII 的一种特定应用程序,因此您至少应该了解如何实现它的概念。

The original ScopeGuard class is included in this Dr. Dobb's article by Andrei Alexandrescu and Petru Marginean. A slightly improved version, with some changes from Joshua Lehrer is available here. (Lehrer's version is the one that I'm using in my projects.) It's also included in the Loki library.

Boost now has a ScopeExit library that's more powerful than ScopeGuard (since it can execute arbitrary code, whereas ScopeGuard can only call a single preexisting function).

Edit: With all of that said, a Scope Guard is really just a specific application of RAII, so you really ought to at least understand the concept of how to implement one.

最佳男配角 2024-09-21 05:13:34

ScopeGuard 已包含在 Loki 库中(在 Modern 中公布) Andrei Alexandrescu 的 C++ 设计,我相信您已经听说过这本伟大的书),并且在我看来已经足够成熟,可以在生产代码中使用。

需要明确的是:我们正在讨论使用 RAII 编写异常安全代码。

附加阅读(在 StackOverflow 上):
使用 ScopeGuard 真的会带来更好的代码吗?

ScopeGuard has been included in the Loki library (advertised in Modern C++ Design by Andrei Alexandrescu, I'm sure you've heard of this great book), and is mature enough to be used in production code, imo.

Just to be clear: We're talking about writing exception safe code using RAII.

Additional reading (on StackOverflow):
Does ScopeGuard use really lead to better code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文