title: Why does making operator new private break std::shared_ptr?
body: <p>I'm implementing a polymorphic type (call it <code>A</code>) that I want to have managed exclusively via <code>std::shared_ptr</code>. To allow using <code>shared_from_this</code> in the constructor of derived classes, <code>A</code> is allocated using <code>new</code> and then initializes a member <code>std::shared_ptr</code> to itself to manage its lifetime automatically. To help enforce this, I've decided to make the class-specific <code>operator new</code> private, and plan to use a <code>create()</code> helper function instead of <code>new</code> and <code>make_shared</code>. The design might look a bit funny but it makes sense in context for the UI library I'm working on. A minimal reproducible example is as follows:</p>
  
  <pre><code>struct A {
      A() : m_sharedthis(this) {
  
      }
  
      void destroy(){
          m_sharedthis = nullptr;
      }
  
      std::shared_ptr&lt;A&gt; self() const {
          return m_sharedthis;
      }
  
  private:
      friend std::shared_ptr&lt;A&gt; create();
  
      std::shared_ptr&lt;A&gt; m_sharedthis;
  };
  
  std::shared_ptr&lt;A&gt; create(){
      auto a = new A();
      return a-&gt;self();
  }
  </code></pre>
  
  <p>This compiles and works fine. The problem arises when I add the following code to the body of <code>A</code>:</p>
  
  <pre><code>struct A {
      ...
  private:
      void* operator new(size_t size){
          return ::operator new(size);
      }
      void operator delete(void* ptr){
          ::operator delete(ptr);
      }
      ...
  };
  </code></pre>
  
  <p>Now this fails to compile on MSVC 2017 with a rather cryptic error message:</p>
  
  <pre><code>error C2664: 'std::shared_ptr&lt;A&gt;::shared_ptr(std::shared_ptr&lt;A&gt; &amp;&amp;) noexcept': cannot convert argument 1 from 'A *' to 'std::nullptr_t'
  note: nullptr can only be converted to pointer or handle types
  </code></pre>
  
  <p>What's going on here? Why is the <code>std::shared_ptr</code> constructor trying to accept only <code>nullptr</code> suddenly?</p>
  
  <p>EDIT: Yes, in the actual code, the class does derive from <code>std::enable_shared_from_this</code>, but it was not necessary to reproduce the error.</p>