Swift for C++ Practitioners: Flexible Array Members
douggregor.net·8h·
Discuss: Hacker News

Flexible array members are an odd feature of C that is intended to capture the pattern of a variable-length structure consisting of a header followed by an array of some element type. It’s a very specific pattern where the last element of a structure can be an array with no size information, like this:

struct Path {
unsigned num_points;
_Bool isClosed;
struct Point points[];
};

struct Point {
double x, y;
};

Flexible array members are not technically part of C++, but are available as an extension in most every C++ compiler because they’re common enough in C that you need them for compatibility with some C libraries.

I recently ran across some Swift code that was using a C API based on flexible array members. The c…

Similar Posts

Loading similar posts...