Can I call `delete` on a vector of pointers in C++ via for_each ?

后端 未结 7 1632
醉梦人生
醉梦人生 2021-01-14 08:32

Suppose I have a std::vector objs (for performance reasons I have pointers not actual Objs).

I populate it with obj.push

7条回答
  •  粉色の甜心
    2021-01-14 09:28

    for_each needs a function pointer or function object. For memory deallocation you could try &::operator delete, which would take the address of the function that deallocates memory. However, when you use the delete statement the compiler calls the destructor before calling operator delete(void*) so cleanup is actually not part of the operator delete(void*) function.

    Use a functor ala GMan's answer.

提交回复
热议问题