#include <iostream>
#include <sstream>

std::stringstream::pos_type size_of_stream(const std::stringstream& ss)
{
    std::streambuf* buf = ss.rdbuf();

    // Get the current position so we can restore it later
    std::stringstream::pos_type original = buf->pubseekoff(0, ss.cur, ss.out);

    // Seek to end and get the position
    std::stringstream::pos_type end = buf->pubseekoff(0, ss.end, ss.out);

    // Restore the position
    buf->pubseekpos(original, ss.out);

    return end;
}

int main()
{
    std::stringstream ss;

    ss << "Hello";
    ss << ' ';
    ss << "World";
    ss << 42;

    std::cout << size_of_stream(ss) << std::endl;

    // Make sure the output string is still the same
    ss << "\nnew line";
    std::cout << ss.str() << std::endl;

    std::string str;
    ss >> str;
    std::cout << str << std::endl;
}