Using smart_pointer and set_new_handler together
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 refine开发者_如何学Cd error handling?
Thanks
The smart pointers that come with C++ (currently just auto_ptr
, but soon including shared_ptr
and weak_ptr
) all use new
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!
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) use new
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 the new
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.
精彩评论