title: How to properly empty stringstream buffer?
body: <p>I have read <a href="https://stackoverflow.com/questions/20731/how-do-you-clear-a-stringstream-variable">How do you clear a stringstream variable?</a> to clear the buffer by <code>stringstream.str(&quot;&quot;)</code>, or in other words - setting empty string. But if I do so, it won't clear it:</p>
  <p><em>date.hpp</em>:</p>
  <pre><code>#include &lt;sstream&gt;
  #include &lt;time.h&gt;
  
  #define fmt &quot;%m/%d/%y&quot;
  
  class Date
  {
      std::stringstream buffer;
      struct tm date;
  
      template &lt;class T&gt;
      void insertbuf(const T &amp;);
      void inserttime();
      std::string getFromIn(std::istream &amp;);
  
  public:
      //ctors
      Date();
      Date(const char *str);
      Date(const std::string &amp;s);
      Date(std::istream &amp;in);
      Date(const Date &amp;d);
  
      //operators
      Date &amp;operator=(const Date &amp;rhs);
  
      //general
      const std::string getAll() const;
      int getMonth() const;
      int getDay() const;
      int getYear() const;
  };
  </code></pre>
  <p><em>date.cpp</em>:</p>
  <pre><code>template &lt;class T&gt;
  void Date::insertbuf(const T &amp;val)
  {
      if (!buffer.rdbuf()-&gt;in_avail())
      {
          buffer &lt;&lt; val;
      }
  }
  
  void Date::inserttime()
  {
      buffer &gt;&gt; std::get_time(&amp;date, fmt);
  }
  
  Date &amp;Date::operator=(const Date &amp;rhs)
  {
      if (&amp;rhs != this)
      {
          buffer.str(&quot;&quot;);
          insertbuf&lt;std::string&gt;(rhs.buffer.str());
          inserttime();
      }
      return *this;
  }
  </code></pre>
  <p>Now in function <code>insertbuf</code>, I will <code>&lt;&lt;</code> to the buffer only if there is no other data. So in the <code>operator=</code>, which its left-hand-side (the object itself, or <code>*this</code>), has some data in the buffer and thus I have to clear them. I am trying to do so by setting the buffer to empty string <code>buffer.str(&quot;&quot;)</code> or equivalently <code>buffer.str(std::string())</code>, but it seems, that will not set it. From here:</p>
  <p><em>main.cpp</em>:</p>
  <pre><code>int main()
  {
      Date bar = &quot;11/23/2020&quot;;
      Date foo = &quot;11/21/2020&quot;;
      cout &lt;&lt; &quot;before operator= &quot; &lt;&lt; foo.getAll() &lt;&lt; endl;
      foo = bar;
      cout &lt;&lt; &quot;after operator= &quot; &lt;&lt; foo.getAll() &lt;&lt; endl;
  }
  </code></pre>
  <p>output:</p>
  <pre><code>before operator= date: 11/21/2020
  after operator= date: 11/21/2020
  </code></pre>
  <p>As I can see the buffer was not cleared in the <code>operator=</code> function, because the buffer did not changed (The output should be <code>11/23/2020</code>, the <code>insertbuf</code> function probably did not get over the <code>if statement</code> because the buffer was not empty, even though I set it to empty string), why? And how to properly clear the buffer of <code>std::stringstream</code> then?</p>
  <p>An exmaple:
  <a href="https://godbolt.org/z/h6zofr" rel="nofollow noreferrer">https://godbolt.org/z/h6zofr</a></p>
  <p>The reason, behind the question is here:
  <a href="https://codereview.stackexchange.com/questions/252456/how-to-implement-simple-date-class-in-c">https://codereview.stackexchange.com/questions/252456/how-to-implement-simple-date-class-in-c</a></p>