std::allocator construct/destroy vs. placement new/p->~T()

前端 未结 2 1615
庸人自扰
庸人自扰 2020-12-31 15:03

For a project of mine, I am writing some STL containers from scratch (I have my reasons). Since I am mimicking the functionality and interfaces of the STL so closely I am do

相关标签:
2条回答
  • 2020-12-31 15:17

    This is just to hide the details of allocation in a method. i.e., we are providing the APIs for construction and destruction, in the future we can change the implementation. Now we are using placement new to allocate the memory, in the future if we want to change the allocation we just have to change these two methods.

    0 讨论(0)
  • 2020-12-31 15:33

    The allocator could add logging statements before and after construction/destruction, or any other side effects it cared to do.

    Of course the actual construction has to occur by calling placement new and the destructor, but it doesn't say in the rulebook that nothing else must happen in the construct/destroy functions

    0 讨论(0)
提交回复
热议问题