Consider the following C99 structure ending with a flexible array element:
struct hdr
{
size_t len;
size_t free;
char buf[];
};
len, for example, gets access using the built-in function (which should be placed in the header file), for example, having bufas an argument:
static inline size_t slen(const char *s)
{
struct hdr *h = (struct hdr*)(s - (int)offsetof(struct hdr, buf));
return h->len;
}
This is the part of the library that needs to be compiled using the C compiler. However, I would like to access this library from C ++; this essentially means that the corresponding header file (with the correct extern "C" {...}guard) must be valid C ++ code. A possible solution is to define a function slenin the body of the source code, completely avoiding inline code, but this is not optimal.
, ++, , - hdr, .
struct cpp_hdr
{
size_t len;
size_t free;
char buf[1];
}
, () len free; buf.
: ,
static inline size_t slen(const char *s)
{
struct cpp_hdr *h = (struct cpp_hdr*)(s - (int)offsetof(struct cpp_hdr, buf));
return h->len;
}
, ?