milanHrabos
milanHrabos

Reputation: 1965

How to properly empty stringstream buffer?

I have read How do you clear a stringstream variable? to clear the buffer by stringstream.str(""), or in other words - setting empty string. But if I do so, it won't clear it:

date.hpp:

#include <sstream>
#include <time.h>

#define fmt "%m/%d/%y"

class Date
{
    std::stringstream buffer;
    struct tm date;

    template <class T>
    void insertbuf(const T &);
    void inserttime();
    std::string getFromIn(std::istream &);

public:
    //ctors
    Date();
    Date(const char *str);
    Date(const std::string &s);
    Date(std::istream &in);
    Date(const Date &d);

    //operators
    Date &operator=(const Date &rhs);

    //general
    const std::string getAll() const;
    int getMonth() const;
    int getDay() const;
    int getYear() const;
};

date.cpp:

template <class T>
void Date::insertbuf(const T &val)
{
    if (!buffer.rdbuf()->in_avail())
    {
        buffer << val;
    }
}

void Date::inserttime()
{
    buffer >> std::get_time(&date, fmt);
}

Date &Date::operator=(const Date &rhs)
{
    if (&rhs != this)
    {
        buffer.str("");
        insertbuf<std::string>(rhs.buffer.str());
        inserttime();
    }
    return *this;
}

Now in function insertbuf, I will << to the buffer only if there is no other data. So in the operator=, which its left-hand-side (the object itself, or *this), 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 buffer.str("") or equivalently buffer.str(std::string()), but it seems, that will not set it. From here:

main.cpp:

int main()
{
    Date bar = "11/23/2020";
    Date foo = "11/21/2020";
    cout << "before operator= " << foo.getAll() << endl;
    foo = bar;
    cout << "after operator= " << foo.getAll() << endl;
}

output:

before operator= date: 11/21/2020
after operator= date: 11/21/2020

As I can see the buffer was not cleared in the operator= function, because the buffer did not changed (The output should be 11/23/2020, the insertbuf function probably did not get over the if statement because the buffer was not empty, even though I set it to empty string), why? And how to properly clear the buffer of std::stringstream then?

An exmaple: https://godbolt.org/z/h6zofr

The reason, behind the question is here: https://codereview.stackexchange.com/questions/252456/how-to-implement-simple-date-class-in-c

Upvotes: 0

Views: 489

Answers (1)

Alan Birtles
Alan Birtles

Reputation: 36399

The problem you are having is that calling buffer.str("") doesn't reset the stream state flags so if a previous operation failed or reached the end of the stream no other reads will succeed. The following should fix the issue:

buffer.str("");
buffer.clear();

Your code seems unnecessarily convoluted. If all you are trying to do is convert dates to and from strings I recommend using Howard Hinnant's date library which is now also part of c++20.

Upvotes: 2

Related Questions