Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is nothing wrong with it except there is no overloaded &lt;&lt; operator defined for it. The existing overloads for &lt;&lt; are expecting a manipulator with the signature <strong>ostream&amp; (*fp)(ostream&amp;)</strong>. </p> <p>If you gave it a manipulator with the type <strong>ostream&amp; (*fp)()</strong> you would get a compiler error since it <strong>does not</strong> have a definition for <strong>operator&lt;&lt;(ostream&amp;, ostream&amp; (*fp)())</strong>. If you wanted this functionality you would have to overload the &lt;&lt; operator to accept manipulators of this type.</p> <p>You would have to write a definition for this: <br> <strong>ostream&amp; ostream::operator&lt;&lt;(ostream&amp; (*m)())</strong></p> <p>Keep in mind here that nothing magical is happening here. The stream libraries rely heavily on <em>standard</em> C++ features: operator overloading, classes and references. </p> <p>Now that you know how you can create the functionality you described, here's why we don't: </p> <p>Without passing a reference to the stream we are trying to manipulate, we can't make modifications to the stream connected to final device (cin, out, err, fstream, etc). The function (modifier's are all just functions with fancy names) would either have to return a new ostream that had nothing to do with the one to the left of the &lt;&lt; operator, or through some very ugly mechanism, figure out which ostream it should connect with else everything to right of the modifier wont make it to the final device, but would rather be sent to whatever ostream the function/modifier returned.</p> <p>Think of streams like this</p> <pre><code>cout &lt;&lt; "something here" &lt;&lt; tab &lt;&lt; "something else"&lt;&lt; endl; </code></pre> <p>really means</p> <pre><code>(((cout &lt;&lt; "something here") &lt;&lt; tab ) &lt;&lt; "something else" ) &lt;&lt; endl); </code></pre> <p>where each set of parentheses does something to cout (write, modify etc) and then returns cout so the next set of parentheses can work on it. </p> <p>If your tab modifier/function did not take a reference to an ostream it would have to somehow guess what ostream was to left of the &lt;&lt; operator to perform its task. Were you working with cour, cerr, some file stream...? The internals of the function will never know unless they are handed that information some how, and why not that how be as simple as a reference to it. </p> <p>Now to really drive the point home, let's look at what <strong>endl</strong> really is and which overloaded version of the &lt;&lt; operator we are using:</p> <p>This operator looks like this:</p> <pre><code> ostream&amp; ostream::operator&lt;&lt;(ostream&amp; (*m)(ostream&amp;)) { return (*m)(*this); } </code></pre> <p>endl looks like this:</p> <pre><code> ostream&amp; endl(ostream&amp; os) { os &lt;&lt; '\n'; os.flush(); return os; } </code></pre> <p>The purpose of endl is to add a newline and flush the stream, making sure all the contents of the stream’s internal buffer have been written to the device. In order to do this, it first needs to write a '\n' to this stream. It then needs to tell the stream to flush. The only way for endl to know which stream to write to and flush is for the operator to pass that information to the endl function when it calls it. It'd be like me telling you to wash my car, but never telling you which car is mine in the full parking lot. You'd never be able to get your job done. You need me to either hand you my car or I can wash it myself. </p> <p>I hope that clears things up</p> <p>PS - If you do happen to accidentally find my car, please wash it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload