Steve Lorimer
Steve Lorimer

Reputation: 28689

std compliant stringstream using stack allocated storage?

Can't seem to find anything currently available in the wild, which seems odd.

So before I roll my own, does anyone know of a std compliant stringstream which allocates storage on the stack?

I'm thinking of 2 ways to achieve this:

Initially use a statically sized buffer on the stack (probably a template parameter to allow compile-time customization)

  1. When space runs out revert to using dynamic storage
  2. When space runs out use alloca to increase the stack storage

This will allow quick input into a stringstream for strings shorter than the predetermined size.

A suitable choice for the initial size will mean that things like logging can be achieved without frequent resizing from the heap.

Upvotes: 4

Views: 1828

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283793

You can get the same benefits by using std::basic_stringbuf<char, char_traits<char>, pooled_allocator>, where you only have to write the pooled allocator.

Then just create a basic_iostream attached to that buffer.

Or, create a new class derived from basic_streambuf.

But don't rewrite stringstream. The iostreams library is designed by extension by replacing the buffer object.

Upvotes: 4

Related Questions