zserio icon indicating copy to clipboard operation
zserio copied to clipboard

[C++] Automatically determine size of arrays

Open frank-aurich opened this issue 5 years ago • 1 comments

When creating objects that contain array members, we currently have to explicitly set the number of elements in the array, even though the count is implictly available internally.

struct Line
{
   varsize numPositions : numPositions > 0;
   Position pn[numPositions];
}

The generated C++ code for the above object allows us to write something like this:

BitStreamWriter bsw;
Line line;
line.setPn({Position(20, 50)});
line.write(bsw);

This compiles just fine, but will throw an exception at runtime, because numPositions was not explicitly set.

Instead we have to write:

BitStreamWriter bsw;
Line line;
line.setPn({Position(20, 50)});
line.setNumPositions(1);
line.write(bsw);

This is cumbersome and error-prone. Since the Line object in C++ internally holds pn as an std::vector, the element count is implicitly available and should be deduced when writing the object.

frank-aurich avatar Dec 10 '20 09:12 frank-aurich

We could think about this but only for such trivial array size expressions. Unfortunately, we are not able to find out any generic solution for that. Consider the following schema:

struct Line
{
   varsize numPositionsA : numPositionsA > 0;
   varsize numPositionsB : numPositionsB > 0;
   Position pn[numPositionsA + numPositionsB];
}

Now, we are not able to deduce from size of pn array what is numPositionsA and what is numPositionsB.

mikir avatar Dec 10 '20 12:12 mikir

This is a duplicate of #556.

mikir avatar Jan 17 '24 12:01 mikir