title: How does counter decrement of smart pointer work?
body: <p>Below is a sample code for shared pointer. I define a shared pointer inside curly braces scope. sp1 is initialized by new A (counter = 1) and sp1 is assigned to sp2 (copy and/or assignment increase the counter by 1 so, counter = 2). I always thought destructor of the A is called when counter becomes 0. But in my case the smart pointer when is about to get out of scope ( via the curly brace ) the counter is two. </p>
  
  <p>My question is : who changed the counter from 2 to 0?  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  using namespace std;
  
  class A{
  public:
      ~A(){
  
          std::cout &lt;&lt; "~A" &lt;&lt;  std::endl;
      }    
  };
  int main(){
      {
          shared_ptr&lt;A&gt; sp1 (new A); 
          shared_ptr&lt;A&gt; sp2 = sp1;
  
          std::cout &lt;&lt; "sp1 count = " &lt;&lt; sp1.use_count() &lt;&lt; std::endl;
          std::cout &lt;&lt; "sp2 count = " &lt;&lt; sp2.use_count() &lt;&lt; std::endl;
      }
  
  return 0;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> 
       Link to paper on <a href="http://www.umich.edu/~eecs381/handouts/C++11_smart_ptrs.pdf" rel="nofollow">smart pointer</a></p>
  
  <p>output:</p>
  
  <hr>
  
  <blockquote>
    <p>sp1 count = 2  </p>
    
    <p>sp2 count = 2</p>
    
    <p>~A</p>
  </blockquote>