title: How do stream manipulators with arguments work?
body: <p>In Stroustrup's C++ book, there is an example of a custom manipulator taking an argument (pls see the attached code). I am confused about how the struct is created. In particular, it looks like there are two int arguments for the constructor of "smanip", one for the function pointer "ff", one for "ii". I don't understand how the int argument is passed to create the structure by using:</p>
  
  <pre><code>cout &lt;&lt; setprecision(4) &lt;&lt; angle;
  </code></pre>
  
  <p>In addition, what is the order these functions get called, and how the the type arguments Ch and Tr are determined?  Thanks a lot.</p>
  
  <pre><code>// manipulator taking arguments
  struct smanip{
      iso_base&amp; (*f) (ios_base&amp;, int);
      int i;
      smanip(ios_base&amp; (*ff)(ios_base&amp;, int), int ii) : f(ff), i(ii){}
  };
  
  template&lt;cladd Ch, class Tr&gt;
  ostream&lt;Ch, Tr&gt;&amp; operator&lt;&lt;(ostream&lt;Ch, Tr&gt;&amp; os, smanip&amp; m){
      return m.f(os, m.i);
  }
  
  ios_base&amp; set_precision(ios_base&amp; s, int n){
      return s.setprecision(n); // call the member function
  }
  
  inline smanip setprecision(int n){
      return smanip(set_precision,n);
  }
  
  // usage:
  cout &lt;&lt; setprecision(4) &lt;&lt; angle;
  </code></pre>