Search code examples
c++c++11new-operatorc++14dynamic-memory-allocation

Are new and delete still useful in C++14?


Given availability of make_unique and make_shared, as well as automatic deletion by unique_ptr and shared_ptr destructors, what are the situations (apart from supporting legacy code) for using new and delete in C++14?


Solution

  • While smart pointers are preferable to raw pointers in many cases, there are still lots of use-cases for new/delete in C++14.

    If you need to write anything that requires in-place construction, for example:

    • a memory pool
    • an allocator
    • a tagged variant
    • binary messages to a buffer

    you will need to use placement new and, possibly, delete. No way around that.

    For some containers that you want to write, you may want to use raw pointers for storage.

    Even for the standard smart pointers, you will still need new if you want to use custom deleters since make_unique and make_shared don't allow for that.