Boost logo

Boost :

Subject: Re: [boost] Interest in a simple buffer abstraction?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2011-01-27 08:42:33

On 27/01/2011 13:37, Boris Kolpackov wrote:

> const char* p = (); | const char* p =&b[0];

That's actually undefined behaviour if b doesn't have at least 1 element.
In C++0x std::vector has a data() member just like your buffer type.

> b.copy (data, size); | b.assign (data, data + size);

How about std::vector<char> a; std::vector<char> b = a;

> |
> b.append (data, size); | b.insert (b.end (), data, data + size);

Appending is more of a string thing. Are you sure you want this?

> Also, you cannot pass the data to vector as void* which will be possible
> with the buffer class, for example:
> void* data;
> ...
> buffer b (data, size); // Ok.
> vbuffer vb (data, data + size); // Error.

Beware of the strict aliasing rules.

> Plus, some natural to have functions are not available in vector, for
> example:
> buffer b (size, capacity);
> buffer b (data, size, capacity);
> Also, for a binary buffer it is nice to be able to assume ownership of
> an existing raw buffer and to detach the underlying raw buffer.

And how is that allowed by your interface?
Also, how do you take care of how the memory is allocated when copying
said buffer?

> Finally, the implementation of std::vector<char> may not be as efficient
> (e.g., using loops to copy elements instead of memcpy, etc).

std::vector can detect PODs.

Boost list run by bdawes at, gregod at, cpdaniel at, john at