Boost logo

Boost :

From: David Manura (dm.list_at_[hidden])
Date: 2006-07-20 11:52:38


Steven Burns <royalstream <at> hotmail.com> writes:
> Recently I came up with a modified version of boost::array that supports
> runtime sized arrays....boost::array<int> darray(10);

Something I think would be useful is an implementation similar to
std::vector that uses a small-string like optimization, where the
size of the stack buffer is a template parameter. For example,

  myarray<char, 10> v; // allocates 10 bytes on the stack
  for(int n=0; n<9; n++) v.push_back('.'); // no heap allocations
  for(int n=0; n<9; n++) v.push_back('.'); // triggers realloc from heap

This has a few advantages:

(1) For <= 10 chars, it requires no heap allocation and therefore
    is fast for some things (similar to C arrays and boost::array).
(2) It can grow beyond 10 chars via heap allocation (like
    std::vector but unlike boost::array).
(3) It maintains both variable size and capacity (like std::vector
    but unlike boost::array).

In fact, this is the approach I'm taking to reimplement basic_string
via flex_string (http://erdani.org/code/main.html), though I had patch
flex_string so that it would work with N > 127 :

  template <size_t N> struct sstring {
    typedef flex_string<
      char,
      std::char_traits<char>,
      std::allocator<char>,
      SmallStringOpt<SimpleStringStorage<char>, N>
> type;
  };
  sstring<500>::type s; // usage

Perhaps there could be a "flex_vector" as well?

  flex_vector<
    int,
    std::allocator<int>,
    SmallVectorOpt<SimpleVectorStorage<int>, N>
>

BTW, is there any chance that flex_string might be added to Boost?
I see a forked version of it included as a utility in Boost Wave, but it's not a
formal part of Boost itself, and it would be nice to have a single version of it
maintained.

--davidm


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk