在升压或C ++ 0x中的任何RAII模板 [英] Any RAII template in boost or C++0x

查看:108
本文介绍了在升压或C ++ 0x中的任何RAII模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 RAII 升压提供任何模板。还有像的scoped_ptr 类,的shared_ptr 这对指针的基本工作原理。这些类可用于除指针以外的任何其他资源。是否有其与一般资源工作的模板。

Is there any template available in boost for RAII. There are classes like scoped_ptr, shared_ptr which basically work on pointer. Can those classes be used for any other resources other than pointers. Is there any template which works with a general resources.

就拿它是在一个范围内的收购之初并已在范围年底莫名其妙地释放了一些资源。双方获取和释放采取一些措施。我们可以写一个模板,它需要做这个任务的两个(或也许一个对象)仿函数。我还没有想通了如何可以做到这一点,我只是想知道是否有任何现有的方法来做到这一点。

Take for example some resource which is acquired in the beginning of a scope and has to be somehow released at the end of scope. Both acquire and release take some steps. We could write a template which takes two(or maybe one object) functors which do this task. I havent thought it through how this can be achieved, i was just wondering are there any existing methods to do it

编辑:如何之一的C ++ 0x为lambda函数的支持

How about one in C++0x with support for lambda functions

推荐答案

的shared_ptr 提供了指定的定制删除。当指针需要被破坏,删除器将被调用,并可以做任何清理行动是必要的。这种方式比简单的指针复杂的资源可以用这个智能指针类进行管理。

shared_ptr provides the possibility to specify a custom deleter. When the pointer needs to be destroyed, the deleter will be invoked and can do whatever cleanup actions are necessary. This way more complicated resources than simple pointers can be managed with this smart pointer class.

这篇关于在升压或C ++ 0x中的任何RAII模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆