一起使用 smart_pointer 和 set_new_handler
是否需要担心将 set_new_handlers 与智能或自动指针结合使用,或者库中包含具有精致错误处理功能的处理程序?
谢谢
Do one need to worry about using set_new_handlers in conjunction with smart or auto pointer's or the libraries come packed with handlers with refined error handling?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++ 附带的智能指针(目前只有
auto_ptr
,但很快包括shared_ptr
和weak_ptr
)都使用new
默认情况下,因此它们将自动与全局新处理程序交互。您不需要做任何特殊的事情来确保在使用这些类时调用新的处理程序。希望这有帮助!
The smart pointers that come with C++ (currently just
auto_ptr
, but soon includingshared_ptr
andweak_ptr
) all usenew
by default, so they'll interface with the global new handler automatically. You don't need to do anything special to ensure that the new handler is invoked when using these classes.Hope this helps!
我认为你正在担心一些不相关的事情。 new 处理程序是一个细节,指定operator new 在找不到足够的内存来分配的情况下如何操作。智能指针是库提供的容器类。大多数 C++ 标准库容器需要分配内存,它们通过分配器类以模块化方式进行分配。默认分配器可能(并且通常确实)使用 new 来获取内存,但是内存分配和对象构造的过程是解耦的,并且任何库类中都不需要知道如何分配内存的任何细节。
new
运算符有效,反之亦然。因此,无论您对 new 运算符做什么(例如通过安装处理程序),都将透明地适用于标准库构造,并且任何一方都无需了解另一方。
I think you're worrying about unrelated things. The new handler is a detail that specifies how
operator new
operates in the event that it cannot find enough memory to allocate. A smart pointer is a library-provided container class. Most C++ standard library containers need to allocate memory, which they do in a modular fashion by means of an allocator class. The default allocator may (and usually does) usenew
to obtain memory, but the process of memory allocation and object construction is decoupled, and there's nothing in any library class that needs to know any details of how thenew
operator works, and vice versa.So whatever you do to your
new
operator (e.g. by installing handlers) will transparently work for standard library constructions, and there is nothing either side has to know about the other.