Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The asio::buffer function has (void*, size_t) and (PodType(&)[N]) overloads. I d

ID: 653097 • Letter: T

Question

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));

Explanation / Answer

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote