RE: [asio-users] ..._buffer_container_1
Brought to you by:
chris_kohlhoff
From: Alterman, E. <Eugene.Alterman@Staples.com> - 2005-11-22 21:49:26
|
Hi Chris, Thank you for reply. > -----Original Message----- > From: Christopher Kohlhoff [mailto:ch...@ko...] > Sent: Monday, November 21, 2005 8:09 PM > To: Alterman, Eugene; 'asi...@li...' > Subject: Re: [asio-users] ..._buffer_container_1 > > > Hi Eugene, > > --- "Alterman, Eugene" <Eugene.Alterman@Staples.com> wrote: > > > I think it is better to have (mutable_/const_)buffer_conteiner_1 > > derived > > from boost::array<..._buffer, 1>. Then the only thing you need to do > > is to add construction from a buffer: > > > > class mutable_buffer_container_1 > > : public boost::array<mutable_buffer, 1> > > { > > public: > > /// Construct to represent a single modifiable buffer. > > explicit mutable_buffer_container_1(const mutable_buffer& b) > > { > > *begin() = b; > > } > > }; > > The problem was that I also wanted the return value of asio::buffer() to be > assignable directly into a const_buffer or mutable_buffer, e.g.: > > asio::const_buffer b = asio::buffer(data, size); > > or > > asio::mutable_buffer b = asio::buffer(data, size); > > Furthermore, in the case of asio::buffer() returning a > mutable_buffer_container_1, I want the "conversion" to mutable_buffer to be > a better conversion than to const_buffer. > > Otherwise you get ambiguities with code like: > > void foo(const mutable_buffer&); > void foo(const const_buffer&); > > char data[1024]; > foo(asio::buffer(data)); > > Using the inheritance here was the only way I could find to make this the > "better" conversion. I'd be happy to know about alternative ways of doing > it. I see. I think you can use Andrei Alexandrescu's mojo trick: Have mutable_buffer derived from const_buffer, and in mutable_buffer_container_1 declare operators: operator const_buffer() const; operator mutable_buffer(); This will give you the desired effect. See the discussion: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/ c6fe15957ab6a261 > > Aside from more code, there is another problem with the current > > approach - 'end()' is defined as 'begin() + 1', but you can use > > pointer arithmetic > > legally only within an array. > > According to the C++ standard, in section 5.7/4 which discusses pointer > arithmetic: > > "For the purposes of these operators, a pointer to a nonarray object behaves > the same as a pointer to the first element of an aray of length one with the > type of the object as its element type." > > which i took to mean it was legal. I could be wrong though :) Of course I was wrong here :( Eugene. P.S. Great library! Thanks. |