On Wednesday, 16 April 2014 at 23:15:43 UTC, Jeroen Bollen wrote:
Is it possible to have a structure with a dynamic size? The
structure would contain an array.
I know I can use templates, but the size won't be known at
compile time. I also know I could just put a dynamic array into
it, but that way it would just be a pointer.
I know there would be major issues like how to pass the struct
to a function, as it has an unknown size, but to be quite
honest I just want pretty code. I'm doing network related
operations. I was hoping there'd still be a way to do this
using templates or so? I just don't want to go through the
hassle of writing a constructor for it to fill in all the
fields, and a toByteArray method to convert it back to raw data.
struct MyStruct {
ulong length;
ubyte[length] data; // obv won't compile
}
Dynamic structs are impossible with D's static type system.
However I suspect vibe's BSON module [1] is pretty close to what
you're looking for; it has a nice API and can easily be sent in
binary form.
[1] http://vibed.org/api/vibe.data.bson/