<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Do we need std::buffer?</title>
	<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/</link>
	<description>Boris Kolpackov's blog about software</description>
	<pubDate>Sun, 05 Apr 2026 17:33:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: loodot_chris</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1798</link>
		<author>loodot_chris</author>
		<pubDate>Thu, 11 Aug 2011 21:43:49 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1798</guid>
		<description>i agree that  char *  is a good approach.</description>
		<content:encoded><![CDATA[<p>i agree that  char *  is a good approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kim Gräsman</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1797</link>
		<author>Kim Gräsman</author>
		<pubDate>Wed, 10 Aug 2011 19:40:01 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1797</guid>
		<description>Boris,

reserve, that's it! Makes sense to follow that convention, I think.

I don't really see why I would use a buffer over a vector if I needed a growable thing, but you may have a point.

std::string is often criticized for its excessively wide interface, so I was trying to save you from that ;-)

Another thing occurred to me: detach() sounds a lot like auto_ptr's release(). I like detach better, but it feels like a Microsoftism--Their ATL library uses it consistently.

For what it's worth,
- Kim</description>
		<content:encoded><![CDATA[<p>Boris,</p>
<p>reserve, that&#8217;s it! Makes sense to follow that convention, I think.</p>
<p>I don&#8217;t really see why I would use a buffer over a vector if I needed a growable thing, but you may have a point.</p>
<p>std::string is often criticized for its excessively wide interface, so I was trying to save you from that ;-)</p>
<p>Another thing occurred to me: detach() sounds a lot like auto_ptr&#8217;s release(). I like detach better, but it feels like a Microsoftism&#8211;Their ATL library uses it consistently.</p>
<p>For what it&#8217;s worth,<br />
- Kim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Kolpackov</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1796</link>
		<author>Boris Kolpackov</author>
		<pubDate>Wed, 10 Aug 2011 12:11:29 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1796</guid>
		<description>Marco, boost::asio::buffer is a wrapper (it is actually a function, not a class) that gives a unified (but limited) interface to various underlying buffer representation (e.g., {void*, size_t} tuple, std::vector, etc) so that they can all be used with ASIO. In particular, it does not manage the underlying memory. So in this sense, the buffer class I am proposing would be just one of the underlying buffers that boost::asio::buffer class would wrap:

buffer b(128);
sock.receive(boost::asio::buffer(b));</description>
		<content:encoded><![CDATA[<p>Marco, boost::asio::buffer is a wrapper (it is actually a function, not a class) that gives a unified (but limited) interface to various underlying buffer representation (e.g., {void*, size_t} tuple, std::vector, etc) so that they can all be used with ASIO. In particular, it does not manage the underlying memory. So in this sense, the buffer class I am proposing would be just one of the underlying buffers that boost::asio::buffer class would wrap:</p>
<p>buffer b(128);<br />
sock.receive(boost::asio::buffer(b));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Kolpackov</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1795</link>
		<author>Boris Kolpackov</author>
		<pubDate>Wed, 10 Aug 2011 11:57:04 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1795</guid>
		<description>Michael, I disagree. char* is an idiomatic way to represent a sequence of bytes in C and C++ programs. The buffer interface only uses void* as input type in order to allow you to populate the buffer with data of some other type without requiring an explicit conversion.

Changing the underlying buffer type from char* to void* would make using the buffer much less convenient. For example, to get the pointer to the 10th byte, now we can write:

char* p = b.data () + 10;

If the underlying buffer type were void*, we would have had to write this instead:

void* p = static_cast&lt;char*&gt; (b.data ()) + 10;

Also note that char* will always implicitly convert to void* so if you want you can use the buffer class as if the underlying buffer type were void*:

void* p = b.data ();</description>
		<content:encoded><![CDATA[<p>Michael, I disagree. char* is an idiomatic way to represent a sequence of bytes in C and C++ programs. The buffer interface only uses void* as input type in order to allow you to populate the buffer with data of some other type without requiring an explicit conversion.</p>
<p>Changing the underlying buffer type from char* to void* would make using the buffer much less convenient. For example, to get the pointer to the 10th byte, now we can write:</p>
<p>char* p = b.data () + 10;</p>
<p>If the underlying buffer type were void*, we would have had to write this instead:</p>
<p>void* p = static_cast<char *> (b.data ()) + 10;</p>
<p>Also note that char* will always implicitly convert to void* so if you want you can use the buffer class as if the underlying buffer type were void*:</p>
<p>void* p = b.data ();</char></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Kolpackov</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1794</link>
		<author>Boris Kolpackov</author>
		<pubDate>Wed, 10 Aug 2011 11:49:09 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1794</guid>
		<description>Norbert, no, 

std::vector&lt;char&gt; v;
v.reserve(1024);

will give a vector of size 0 and of capacity 1024 (or greater). Strictly speaking, you still cannot access the underlying buffer because the vector is empty. Also, if later you do something like v.resize(1024) or v.insert(v.end(), data, data + size), all your data will be overwritten.</description>
		<content:encoded><![CDATA[<p>Norbert, no, </p>
<p>std::vector<char> v;<br />
v.reserve(1024);</p>
<p>will give a vector of size 0 and of capacity 1024 (or greater). Strictly speaking, you still cannot access the underlying buffer because the vector is empty. Also, if later you do something like v.resize(1024) or v.insert(v.end(), data, data + size), all your data will be overwritten.</char></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Kolpackov</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1793</link>
		<author>Boris Kolpackov</author>
		<pubDate>Wed, 10 Aug 2011 11:39:34 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1793</guid>
		<description>Kim, thanks for the feedback on the buffer interface. 

I agree the size() and capacity() modifiers are a bit unconventional. The alternatives would be to follow the std container/string naming and call them resize() and reserve(). I think it is a good idea to have both size and capacity; the buffer may contain only so much data (size) but you may want to allocate a larger chunk of memory than what is currently needed (capacity) in order to be able to grow without reallocations.

Regarding the find() redundancy, I disagree: b.find('x') is terser than find(b.data(), b.data() + b.size(), 'x'). Also std::string has find() even though you can use std::find() with it as well.

Regarding iterators, yes, I initially added them using the raw pointer as the iterator type. This may not be the preferred approach for std containers so it may have to be wrapped into a class-type. But I agree, iterators are a good idea.

Same about movable -- the C++-0x version should definitely support this. I wonder if there is a portable way to detect the C++-0x mode at compile time?</description>
		<content:encoded><![CDATA[<p>Kim, thanks for the feedback on the buffer interface. </p>
<p>I agree the size() and capacity() modifiers are a bit unconventional. The alternatives would be to follow the std container/string naming and call them resize() and reserve(). I think it is a good idea to have both size and capacity; the buffer may contain only so much data (size) but you may want to allocate a larger chunk of memory than what is currently needed (capacity) in order to be able to grow without reallocations.</p>
<p>Regarding the find() redundancy, I disagree: b.find(&#8217;x') is terser than find(b.data(), b.data() + b.size(), &#8216;x&#8217;). Also std::string has find() even though you can use std::find() with it as well.</p>
<p>Regarding iterators, yes, I initially added them using the raw pointer as the iterator type. This may not be the preferred approach for std containers so it may have to be wrapped into a class-type. But I agree, iterators are a good idea.</p>
<p>Same about movable &#8212; the C++-0x version should definitely support this. I wonder if there is a portable way to detect the C++-0x mode at compile time?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marco Craveiro</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1792</link>
		<author>Marco Craveiro</author>
		<pubDate>Wed, 10 Aug 2011 11:33:52 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1792</guid>
		<description>Hi Boris,

how does this buffer class relate to the Asio buffer:

http://www.boost.org/doc/libs/1_47_0/boost/asio/buffer.hpp

it would be nice to have only one...

Cheers

Marco</description>
		<content:encoded><![CDATA[<p>Hi Boris,</p>
<p>how does this buffer class relate to the Asio buffer:</p>
<p><a href="http://www.boost.org/doc/libs/1_47_0/boost/asio/buffer.hpp" rel="nofollow">http://www.boost.org/doc/libs/1_47_0/boost/asio/buffer.hpp</a></p>
<p>it would be nice to have only one&#8230;</p>
<p>Cheers</p>
<p>Marco</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael S.</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1791</link>
		<author>Michael S.</author>
		<pubDate>Wed, 10 Aug 2011 08:44:16 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1791</guid>
		<description>I don't particular like the use of char* type to represent the data (buffer memory). You're already using both char* and void* which is confusing.

The buffer data is not a sequence of characters. If anything it is a sequence/block of bytes.

I'll suggest introducing a typedef to represent that, e.g.

class buffer
{
public:
  typedef void * bufptr; // or bufdata or memdata or memptr or ...
...

};</description>
		<content:encoded><![CDATA[<p>I don&#8217;t particular like the use of char* type to represent the data (buffer memory). You&#8217;re already using both char* and void* which is confusing.</p>
<p>The buffer data is not a sequence of characters. If anything it is a sequence/block of bytes.</p>
<p>I&#8217;ll suggest introducing a typedef to represent that, e.g.</p>
<p>class buffer<br />
{<br />
public:<br />
  typedef void * bufptr; // or bufdata or memdata or memptr or &#8230;<br />
&#8230;</p>
<p>};</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Norbert</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1789</link>
		<author>Norbert</author>
		<pubDate>Tue, 09 Aug 2011 20:42:18 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1789</guid>
		<description>std::vector v(1024); initializes memory, ok. But what would
std::vector v; v.reserve(1024); do?

OK, it's more text to write, but wouldn't that give you an uninitialized buffer of size 1024?</description>
		<content:encoded><![CDATA[<p>std::vector v(1024); initializes memory, ok. But what would<br />
std::vector v; v.reserve(1024); do?</p>
<p>OK, it&#8217;s more text to write, but wouldn&#8217;t that give you an uninitialized buffer of size 1024?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Kolpackov</title>
		<link>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1788</link>
		<author>Boris Kolpackov</author>
		<pubDate>Tue, 09 Aug 2011 19:27:13 +0000</pubDate>
		<guid>https://codesynthesis.com/~boris/blog//2011/08/09/do-we-need-std-buffer/#comment-1788</guid>
		<description>I just re-checked the VC++ 10 implementation and it appears it does have the POD optimization.</description>
		<content:encoded><![CDATA[<p>I just re-checked the VC++ 10 implementation and it appears it does have the POD optimization.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
