title: How does std::shared_ptr delete a polymorphic type with protected destructor?
body: <p>In the following example:</p>
  
  <pre><code>class Base {
      protected:
      ~Base() {
          cout &lt;&lt; "B\n";
      }
  };
  
  class Derived : public Base {
      public:
      virtual ~Derived(){
          cout &lt;&lt; "D\n";
      }
  };
  
  
  int main() {
      Base *myBase_raw = new Derived();
      delete myBase_raw;
      return 0;
  }
  </code></pre>
  
  <p>you expect to get:</p>
  
  <blockquote>
    <p>error: ‘virtual Base::~Base()’ is protected within this context delete myBase_raw;</p>
  </blockquote>
  
  <p>Which is fine and a useful tool for stopping people from deleting your objects. This is also the case with <code>std::unique_ptr</code>. But when you swap the raw pointer for a <code>std::shared_ptr</code>, it compiles correctly:</p>
  
  <pre><code>int main() {
      std::shared_ptr&lt;Base&gt; myBaseS(new Derived());
  }
  </code></pre>
  
  <p>And seemingly runs deleting the object and calling the <code>Base</code> and <code>Derived</code> destructors (<a href="https://ideone.com/68wH9J" rel="noreferrer">Live example</a>). According to <a href="http://en.cppreference.com/w/cpp/memory/shared_ptr" rel="noreferrer">cppreference</a>:</p>
  
  <blockquote>
    <p>The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.</p>
  </blockquote>
  
  <p>Since I provided no custom deleter it should call <code>delete</code>. How is the above possible?</p>