Does std::make_shared() use custom allocators?

前端 未结 2 1753
情书的邮戳
情书的邮戳 2021-02-07 04:19

Consider this code:

#include 
#include 


class SomeClass {
public:
    SomeClass() {
        std::cout << \"SomeClass()\" &l         


        
2条回答
  •  离开以前
    2021-02-07 04:46

    To expand on Mat's correct answer, make_shared is typically implemented by allocating an object that contains the shared_ptr reference counts and a buffer of uninitialized bytes:

    template
      struct shared_count_inplace
      {
        long m_count;
        long weak_count;
        typename std::aligned_storage::type m_storage;
        // ...
      };
    

    This is the type which will be allocated on the heap, not your type, so your type's new is not called. Then your type will be constructed using placement new at the location (void*)&m_storage.

提交回复
热议问题