Using T[1] instead of T for functions overloaded for T(&)[N]
- by Abyx
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));