Re: [asio-users] ConstBufferSequence requirements document question
Brought to you by:
chris_kohlhoff
From: Cheng L. <rhy...@gm...> - 2007-05-28 10:37:59
|
Christopher Kohlhoff wrote: > On Fri, 25 May 2007 16:53:51 +0800, "Cheng Lian" <rhy...@gm...> > said: >> Hi all, >> >> I'm writing some Asio specific type requirements checking code to help >> constructing a ConstBufferSequence compatible class template. I've read >> the following code snippet from the ConstBufferSequence requirements >> documentation: >> >> bool equal_buffer( >> const X::value_type& v1, >> const X::value_type& v2) >> { >> const_buffer b1(v1); // (1) >> const_buffer b2(v2); // (2) >> return >> buffer_cast<const void*>(b1) >> == buffer_cast<const void*>(b2) >> && buffer_size(b1) == buffer_size(b2); >> } >> >> The question is about these two lines (1) & (2). In the header file >> asio/buffer.hpp I found only one const_buffer constructor that takes >> exactly 1 parameter whose prototype is: >> >> const_buffer(const mutable_buffer& b) > > The compiler-generated copy constructor is also a constructor that takes > exactly one parameter :) Well, yes, so const_buffer can only be instantiated with a <ptr, size> pair or another const_buffer (or an instance of a class that has a const_buffer type conversion constructor)? > >> Which implies that v1 should meets the ConvertibleToMutableBuffer >> requirements. Once again, from the ConvertibleToMutableBuffer >> requirements, I read the following code snippets: > > Obviously you can't have ConvertibleToMutableBuffer as part of the > requirements for ConstBufferSequence, since only the conversion > mutable->const is safe, not the other way around. I.e. you shouldn't be > able to get a mutable buffer out of a constant buffer sequence. > Sorry, I didn't express myself clearly. I means v1 must be initialized by an object that meets the ConvertibleToMutableBuffer requirement since const_buffer only have a default copy constructor and a constructor that accepts a mutable_buffer. >> mutable_buffer u(a); >> mutable_buffer v(a); >> ... >> >> But, here's the problem, in asio/buffer.hpp, there's no mutable_buffer >> constructor that takes exactly 1 parameter. And my testing code failed >> to compile until I made the following two changes: > > What type were you instantiating the test code on when it failed to > compile? The initialisation of the form: > I think I've found out my mistake. At first I used a std::string v1 to initialize the following b1. But it's clear that std::string doesn't meets the ConvertibleToMutableBuffer requirement. > const_buffer b1(v1); > > is exactly what is performed inside asio's implementation, so it should > work for a type X to meet the necessary ConstBufferSequence > requirements. Where exactly is the code that you refer to? I checked the buffers example, and in the shared_const_buffer constructor, and in this example, you use boost::asio::buffer() rather than the copy constructor to initialize the buffer_ member. > > Cheers, > Chris > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ |