title: Get the size of a an element in a C++ output stream
body: <p>I am formatting some log output. I want the end result to look like this:</p>
  
  <pre><code>Foo.................................12.1
  Bar....................................4
  Foo Bar............................42.01
  </code></pre>
  
  <p>The total width is constant but both parameters (name and value) have various sizes. Is there a clean way to get the width of a given parameter, once included in a std::ostream?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct Entry {
      std::string name_;
      double value_;
  };
  
  constexpr int line_width = 30;
  
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; log, const Entry&amp; e)
  {
      log &lt;&lt; e.name_
          &lt;&lt; std::string(line_width - e.name_.size(), '.') \\ should subtract the width of e.value_
          &lt;&lt; e.value_;
      return log;
  }
  
  int main()
  {
      Entry foo    = { "Foo", 12.1 };
      Entry bar    = { "Bar", 4};
      Entry foobar = { "Foo Bar", 42.01};
  
      std::cout &lt;&lt; foo &lt;&lt; '\n' &lt;&lt; bar &lt;&lt; '\n' &lt;&lt; foobar &lt;&lt; '\n';
  }
  </code></pre>
  
  <p>The code above won't work because I did not subtract the width of the value. I was thinking about writing a function that would do something like this:</p>
  
  <pre><code>template &lt;typename T&gt;
  int get_width(std::ostream&amp; log, T value)
  {
      // 1. use tellp to get initial stream size
      // 2. insert the value in the stream
      // 3. use tellp to get final stream size  
      // 4. remove the value from the stream (is that even possible?)
      // 5. return the size = final - initial 
  }
  </code></pre>
  
  <p>Is there a clean way to reach my goal?</p>