admin管理员组

文章数量:1406943

I have the following macro:

#define SERINFO(class, ...) static size_t* GetSerializationInfo() \
            { static size_t arr[NARGS(__VA_ARGS__) * 2 + 1] = {NARGS(__VA_ARGS__), MAP(SERINFOMEMBER, class, __VA_ARGS__)}; return arr; }
#define SERINFOMEMBER(class, member) sizeof(member), offsetof(class, member)

Where NARGS calculates the number of variadic arguments and MAP expands to SERINFOMEMBER(class, arg1), SERINFOMEMBER(class, arg2), ...

//delayed join to expand other parts of the expression before concatenating
#define JOIN(x, y) JOIN_(x, y)
#define JOIN_(x, y) x ## y

//counts amount of variadic arguments (max 10 args)
#define NARGS_(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define NARGS(...) NARGS_(_, ## __VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

//expands to 'fn(x, a), fn(x, b), fn(x, c)...' (max 8 args)
#define MAP_1(fn, x, a)                         fn(x, a)
#define MAP_2(fn, x, a, b)                      fn(x, a), fn(x, b)
#define MAP_3(fn, x, a, b, c)                   fn(x, a), fn(x, b), fn(x, c)
#define MAP_4(fn, x, a, b, c, d)                fn(x, a), fn(x, b), fn(x, c), fn(x, d)
#define MAP_5(fn, x, a, b, c, d, e)             fn(x, a), fn(x, b), fn(x, c), fn(x, d), fn(x, e)
#define MAP_6(fn, x, a, b, c, d, e, f)          fn(x, a), fn(x, b), fn(x, c), fn(x, d), fn(x, e), fn(x, f)
#define MAP_7(fn, x, a, b, c, d, e, f, g)       fn(x, a), fn(x, b), fn(x, c), fn(x, d), fn(x, e), fn(x, f), fn(x, g) 
#define MAP_8(fn, x, a, b, c, d, e, f, g, h)    fn(x, a), fn(x, b), fn(x, c), fn(x, d), fn(x, e), fn(x, f), fn(x, g), fn(x, h)
#define MAP(fn, x, ...) JOIN(MAP_, NARGS(__VA_ARGS__)) (fn, x, __VA_ARGS__)

I'm trying to put it in a struct like so:

struct Values
{
    int ValueA;
    int ValueB;
...
    SERINFO(Values, ValueA, ValueB, ...)
};

Now, the problem is, while it correctly expands to ... arr = { 2, sizeof(ValueA), offsetof(Values, ValueA), sizeof(ValueB), offsetof(Values, ValueB) } ... the compiler still complies about offsetof usage for some reason - error C2618: illegal member designator in offsetof. What am I doing wrong?

本文标签: cMSVCerror C2618 illegal member designator in offsetofStack Overflow