title: Why the `T* operator->()` is applied repeatedly even if written once?
body: <p>Why the <code>T* operator-&gt;()</code> is applied repeatedly even if written once? But another <code>T&amp; operator*()</code> is applied once, and should be written many times.</p>
  
  <p>As known there is Execute-Around Pointer Idiom in C++. <a href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Execute-Around_Pointer" rel="nofollow">More C++ Idioms/Execute-Around Pointer</a></p>
  
  <p>Provide a smart pointer object that transparently executes actions before and after each function call on an object, given that the actions performed are the same for all functions. And before and after each treatment to member variable of a class. For example we can performs:</p>
  
  <ul>
  <li>lock mutex</li>
  <li>log action</li>
  <li>visualize changing data</li>
  </ul>
  
  <p>I added some in <code>main()</code> to <a href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Execute-Around_Pointer#Solution_and_Sample_Code" rel="nofollow">this example</a>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  class VisualizableVector {
    public:
      class proxy {
        public:
          proxy (std::vector&lt;int&gt; *v) : vect (v) {
              std::cout &lt;&lt; "Before size is: " &lt;&lt; vect-&gt;size() &lt;&lt; std::endl;
          }
          std::vector&lt;int&gt; * operator -&gt; () { return vect; }
          std::vector&lt;int&gt; &amp; operator * () { return *vect; }
          ~proxy () { std::cout &lt;&lt; "After size is: " &lt;&lt; vect-&gt;size() &lt;&lt; std::endl; }
        private:
          std::vector &lt;int&gt; * vect;
      };        
      VisualizableVector (std::vector&lt;int&gt; *v) : vect(v) {}            
      ~VisualizableVector () { delete vect; }   
      proxy operator -&gt; () { return proxy (vect); }
      proxy operator * () { return proxy (vect); }
    private:
      std::vector &lt;int&gt; * vect;
  };
  
  int main()
  {
    VisualizableVector vecc (new std::vector&lt;int&gt;);
  
    vecc-&gt;push_back (10);         // 1. Note use of -&gt; operator instead of . operator      
    vecc-&gt;push_back (20);         // 2. ok      
    (*vecc)-&gt;push_back (30);      // 3. ok      
    // (*vecc).push_back (40);    // 4. error      
    (**vecc).push_back (50);      // 5. ok      
    // vecc-&gt;-&gt;push_back (60);    // 6. error     
  }
  </code></pre>
  
  <p>Online compiler result: <a href="http://ideone.com/cXGdxW" rel="nofollow">http://ideone.com/cXGdxW</a></p>
  
  <p>Why do we need to write twice <code>**</code>, but only once <code>-&gt;</code> ? </p>
  
  <p>Its operator return the same thing <code>proxy</code>:</p>
  
  <pre><code>    proxy operator -&gt; () { return proxy (vect); }
      proxy operator * () { return proxy (vect); }
  </code></pre>
  
  <p>But why do we need to use <code>*</code> again, but we shouldn't use <code>-&gt;</code> again?:</p>
  
  <pre><code>  vecc-&gt;push_back (20);     // 2. ok      (vecc-&gt;) is proxy
    (**vecc).push_back (50);  // 5. ok      (*vecc) is proxy
  </code></pre>
  
  <p>Why not <code>vecc-&gt;-&gt;push_back (20);</code>?</p>
  
  <p>Is there anything about this in the standard C++ (03/11/14)?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>In different cases we should use 1,2 or 3 <code>operator-&gt;</code>s : <a href="http://ideone.com/89kfYF" rel="nofollow">http://ideone.com/89kfYF</a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;    
  class VisualizableVector {
    public:
      class proxy {
        public:
          proxy (std::vector&lt;int&gt; *v) : vect (v) {
              std::cout &lt;&lt; "Before size is: " &lt;&lt; vect-&gt;size() &lt;&lt; std::endl;
          }
          std::vector&lt;int&gt; * operator -&gt; () { return vect; }
          std::vector&lt;int&gt; &amp; operator * () { return *vect; }
          ~proxy () { std::cout &lt;&lt; "After size is: " &lt;&lt; vect-&gt;size() &lt;&lt; std::endl; }
        private:
          std::vector &lt;int&gt; * vect;
      };        
      VisualizableVector (std::vector&lt;int&gt; *v) : vect(v) {}            
      ~VisualizableVector () { delete vect; }   
      proxy operator -&gt; () { return proxy (vect); }
      proxy operator * () { return proxy (vect); }
    private:
      std::vector &lt;int&gt; * vect;
  };
  
  int main()
  {
    VisualizableVector vecc (new std::vector&lt;int&gt;);
  
      vecc-&gt;push_back(30);            // ok       // one -&gt;
    //vecc.operator-&gt;().push_back(30);// error    // one -&gt;
  
    //vecc-&gt;-&gt;push_back(30);          // error    // two -&gt;
    vecc.operator-&gt;()-&gt;push_back(30); // ok       // two -&gt;
  
    auto proxy3 = vecc.operator-&gt;();      // 1st operator-&gt;()
    auto pointer = proxy3.operator-&gt;();   // 2nd operator-&gt;()
    pointer-&gt;push_back(30);               // 3rd operator-&gt;()      
    return 0;
  }
  </code></pre>
  
  <p>Page 327: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf" rel="nofollow">Working Draft, Standard for Programming Language C++ 2014-11-19</a></p>
  
  <blockquote>
    <p>13.5.6 Class member access [over.ref] 1 operator-> shall be a non-static member function taking no parameters. It implements the
    class member access syntax that uses ->. postfix-expression ->
    templateopt id-expression postfix-expression -> pseudo-destructor-name
    An expression x->m is interpreted as (x.operator->())->m for a class
    object x of type T if T::operator->() exists and if the operator is
    selected as the best match function by the overload resolution
    mechanism (13.3).</p>
  </blockquote>
  
  <p>I.e. <code>x-&gt;m</code> is <code>(x.operator-&gt;())-&gt;m</code>.</p>