Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the same strstream functionality now that's deprecated?

Tags:

c++

I use to write code like this:

void fun(char *buff, unsigned size)
{
    std::strstream str(buff, size);
    str << "hello world: " << 5;
}

so I can use stream output over an arbitrary buffer. I've found this technique both efficient (no allocations) and useful (streams!). Now that std::strstream is deprecated, how can I get the same speed+flexibility that I can get with this code?

like image 597
chila Avatar asked Dec 04 '25 19:12

chila


1 Answers

The standard library doesn't provide that functionality. However, Boost does with its generic streams and array sources/sinks.

Boost Generic Stream
Boost Array Devices

char buff[size];
boost::stream<boost::array_sink> out(buff, size);
out << "Hello, world!";

(Code untested)

like image 66
Peter Alexander Avatar answered Dec 06 '25 09:12

Peter Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!