I just recently learned about Scope Guard C++ idiom. Unfortunately I can't find any good implementation of it.
我刚刚了解了Scope Guard C ++习语。不幸的是我找不到任何好的实现。
Can anyone point me to some good and usable Scope Guard implementation in C++?
有人能指出我在C ++中使用一些好的和可用的Scope Guard实现吗?
Thanks, Boda Cydo.
谢谢,Boda Cydo。
6 个解决方案
#1
8
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.
ScopeGuard已经被包含在Loki库中(由Andrei Alexandrescu在Modern C ++ Design中做广告,我相信你已经听说过这本伟大的书),并且已经足够成熟,可以用于生产代码,imo。
Just to be clear: We're talking about writing exception safe code using RAII.
需要明确的是:我们正在谈论使用RAII编写异常安全代码。
Additional reading (on *): Does ScopeGuard use really lead to better code?
附加阅读(在*上):ScopeGuard的使用是否真的能带来更好的代码?
#2
15
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.
最初的ScopeGuard课程包含在Dr. Dobb的Andrei Alexandrescu和Petru Marginean的文章中。这里有一个稍微改进的版本,有一些Joshua Lehrer的变化。 (Lehrer的版本是我在我的项目中使用的版本。)它也包含在Loki库中。
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).
Boost现在有一个ScopeExit库,它比ScopeGuard更强大(因为它可以执行任意代码,而ScopeGuard只能调用一个预先存在的函数)。
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.
编辑:尽管如此,Scope Guard实际上只是RAII的一个特定应用程序,所以你真的应该至少理解如何实现它的概念。
#3
4
The Folly library (open-source from facebook) also provides an implementation (not surprising since A.A. is employed by them):
Folly库(来自facebook的开源)也提供了一个实现(因为它们使用了A.A.,这并不奇怪):
https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h
I think this and the MNMLSTC implementation mentioned here are both worth consideration.
我认为这和这里提到的MNMLSTC实现都值得考虑。
#4
0
A "Scope Guard" object is just one instance of the much broader RAII idiom.
“范围保护”对象只是更广泛的RAII习语的一个实例。
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.
并没有单一的实施。这是C ++程序员必须理解的东西,而不仅仅是复制/粘贴。幸运的是,实施它也很简单。
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.
你不需要找到“一个好的实现”,因为你将自己编写几十个和几十个不同的实现。它们写起来很简单,并且它们不能轻易地被重用,因为每个都包含不同类型的资源。
#5
0
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.
有人建议将scope_guard添加到标准库中。您可以在此处阅读该论文,其中包括您可以复制/粘贴的示例实现。有关实施,请参见第9.1节。
#6
0
MNMLSTC core has a modern C++11 implementation of the scope guard idiom.
MNMLSTC核心具有现代C ++ 11实现的范围保护惯用语。
#1
8
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.
ScopeGuard已经被包含在Loki库中(由Andrei Alexandrescu在Modern C ++ Design中做广告,我相信你已经听说过这本伟大的书),并且已经足够成熟,可以用于生产代码,imo。
Just to be clear: We're talking about writing exception safe code using RAII.
需要明确的是:我们正在谈论使用RAII编写异常安全代码。
Additional reading (on *): Does ScopeGuard use really lead to better code?
附加阅读(在*上):ScopeGuard的使用是否真的能带来更好的代码?
#2
15
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.
最初的ScopeGuard课程包含在Dr. Dobb的Andrei Alexandrescu和Petru Marginean的文章中。这里有一个稍微改进的版本,有一些Joshua Lehrer的变化。 (Lehrer的版本是我在我的项目中使用的版本。)它也包含在Loki库中。
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).
Boost现在有一个ScopeExit库,它比ScopeGuard更强大(因为它可以执行任意代码,而ScopeGuard只能调用一个预先存在的函数)。
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.
编辑:尽管如此,Scope Guard实际上只是RAII的一个特定应用程序,所以你真的应该至少理解如何实现它的概念。
#3
4
The Folly library (open-source from facebook) also provides an implementation (not surprising since A.A. is employed by them):
Folly库(来自facebook的开源)也提供了一个实现(因为它们使用了A.A.,这并不奇怪):
https://github.com/facebook/folly/blob/master/folly/ScopeGuard.h
I think this and the MNMLSTC implementation mentioned here are both worth consideration.
我认为这和这里提到的MNMLSTC实现都值得考虑。
#4
0
A "Scope Guard" object is just one instance of the much broader RAII idiom.
“范围保护”对象只是更广泛的RAII习语的一个实例。
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.
并没有单一的实施。这是C ++程序员必须理解的东西,而不仅仅是复制/粘贴。幸运的是,实施它也很简单。
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.
你不需要找到“一个好的实现”,因为你将自己编写几十个和几十个不同的实现。它们写起来很简单,并且它们不能轻易地被重用,因为每个都包含不同类型的资源。
#5
0
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.
有人建议将scope_guard添加到标准库中。您可以在此处阅读该论文,其中包括您可以复制/粘贴的示例实现。有关实施,请参见第9.1节。
#6
0
MNMLSTC core has a modern C++11 implementation of the scope guard idiom.
MNMLSTC核心具有现代C ++ 11实现的范围保护惯用语。