Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The asio::buffer function has (void*, size_t) and (PodType(&)[N]) overloads.
I didn't want to write ugly C-style (&x, sizeof(x)) code, so I wrote this:

SomePacket packet[1]; // SomePacket is POD
read(socket, asio::buffer(packet));
foo = packet->foo;

But that packet-> looks kinda weird - the packet is an array after all.
(And packet[0]. doesn't look better.)

Now, I think if it was a good idea to write such code. Maybe I should stick to unsafe C-style code with void* and sizeof?

Upd: here is another example, for writing a packet:

SomePacket packet[1]; // SomePacket is POD
packet->id = SomePacket::ID;
packet->foo = foo;
write(socket, asio::buffer(packet));
share|improve this question
"Now, I think if it was a good idea to write such code" Come again? – Mooing Duck Sep 26 '12 at 19:35
I had to read this like 5 times to figure out what you were doing. Go with the C-style, because I would have only have had to read this once. – Mooing Duck Sep 26 '12 at 19:36
Actually, is an interesting idea as well, and is clearer WRT the buffer and object. – Mooing Duck Sep 26 '12 at 19:50
I'm a bit hazy on P.SE's policies, but wouldn't this be a better fit for SO? ICBWT. – sbi Sep 26 '12 at 19:51
What's ugly about sizeof()? – James Sep 27 '12 at 12:58
up vote 3 down vote accepted

If I were you, I'd create a function:

template< typename PodType >
whatever buffer(PodType& obj) { return buffer(&obj, sizeof(obj)) }

But this is so obvious an omission in the set of overloads provided, that I wonder if I'm missing something.

share|improve this answer
How do you SFINAE on PodType being, well, a POD type? I believe that any type traits that could do that need some sort of compiler assistance. – Lars Viklund Sep 28 '12 at 3:24
@Lars: Isn't there a std::is_pod<> in C++11? (I am not sure, since I haven't played with C++11 yet.) – sbi Sep 29 '12 at 10:36
Yes, you have is_pod in C++11. Boost.Asio hasn't abandoned all the people with C++03 compilers yet. Just because a new language standard appears doesn't mean that everyone magically are on conformant compilers, particularly not for existing products. – Lars Viklund Sep 29 '12 at 16:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.