5

How can I write a macro (for gcc) that would be used like this:

CREATE_STRUCT(my_struct1,foo);
CREATE_STRUCT(my_struct2,foo,bar);

and expands to

struct my_struct1 {
     std::string foo;
};

struct my_struct2 {
     std::string foo;
     std::string bar;
};

?

I certainly do need the flexibility of different number of members, but already a small number would be fine for me (something like 4 or 5).

I found a couple of related questions, eg this and this, but I am completely lost when trying to apply such arcane macro magic to this problem.

PS: I know how I could write 5 macros (one for each number of params) that would do the job, so actually the question is: Is there an "easy" way to write a variadic macro that does the job? On the other hand, I will add more stuff to the structs, so having it all in one place would save lots of boilerplate.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185

3 Answers3

3

Using code from Is it possible to iterate over arguments in variadic macros? , you may do (Hard coded up to 8 arguments):

#define CONCATENATE(arg1, arg2)   CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2)  CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2)  arg1##arg2

#define FOR_EACH_1(what, x) what(x);
#define FOR_EACH_2(what, x, ...)\
  what(x);\
  FOR_EACH_1(what,  __VA_ARGS__)
#define FOR_EACH_3(what, x, ...)\
  what(x);\
  FOR_EACH_2(what, __VA_ARGS__)
#define FOR_EACH_4(what, x, ...)\
  what(x);\
  FOR_EACH_3(what,  __VA_ARGS__)
#define FOR_EACH_5(what, x, ...)\
  what(x);\
 FOR_EACH_4(what,  __VA_ARGS__)
#define FOR_EACH_6(what, x, ...)\
  what(x);\
  FOR_EACH_5(what,  __VA_ARGS__)
#define FOR_EACH_7(what, x, ...)\
  what(x);\
  FOR_EACH_6(what,  __VA_ARGS__)
#define FOR_EACH_8(what, x, ...)\
  what(x);\
  FOR_EACH_7(what,  __VA_ARGS__)

#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__) 
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N 
#define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0

#define FOR_EACH_(N, what, ...) CONCATENATE(FOR_EACH_, N)(what, __VA_ARGS__)
#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)

#define STRING_MEMBERS(x) std::string x

#define CREATE_STRUCT(name, ...) struct name { FOR_EACH(STRING_MEMBERS, __VA_ARGS__) }

CREATE_STRUCT(my_struct1, foo);
CREATE_STRUCT(my_struct2,foo,bar);
Jarod42
  • 203,559
  • 14
  • 181
  • 302
3

This works with any number of arguments:

#define CREATE_STRUCT(N, ...) struct N { std::string __VA_ARGS__; }

Examples:

CREATE_STRUCT(MyStruct, foo);
CREATE_STRUCT(MyStruct, foo, bar);
CREATE_STRUCT(MyStruct, foo, bar, baz);
JustinC
  • 61
  • 5
1

Here is simple implementation, limited to five members.

#define CREATE_STRUCT_IMPL_1(S1) std::string S1;
#define CREATE_STRUCT_IMPL_2(S1, ...) CREATE_STRUCT_IMPL_1(S1) CREATE_STRUCT_IMPL_1(__VA_ARGS__)
#define CREATE_STRUCT_IMPL_3(S1, ...) CREATE_STRUCT_IMPL_1(S1) CREATE_STRUCT_IMPL_2(__VA_ARGS__)
#define CREATE_STRUCT_IMPL_4(S1, ...) CREATE_STRUCT_IMPL_1(S1) CREATE_STRUCT_IMPL_3(__VA_ARGS__)
#define CREATE_STRUCT_IMPL_5(S1, ...) CREATE_STRUCT_IMPL_1(S1) CREATE_STRUCT_IMPL_4(__VA_ARGS__)

#define CREATE_STRUCT_IMPL(_1,_2,_3,_4,_5,NAME,...) NAME

#define CREATE_STRUCT(N, ...) struct N{ CREATE_STRUCT_IMPL(__VA_ARGS__, CREATE_STRUCT_IMPL_5, CREATE_STRUCT_IMPL_4, CREATE_STRUCT_IMPL_3, CREATE_STRUCT_IMPL_2, CREATE_STRUCT_IMPL_1)(__VA_ARGS__) }

Examples

CREATE_STRUCT(my_struct1, foo);
CREATE_STRUCT(my_struct2, foo, bar);
CREATE_STRUCT(my_struct3, foo, bar, meow);
CREATE_STRUCT(my_struct4, foo, bar, meow, bazz);
CREATE_STRUCT(my_struct5, foo, bar, meow, bazz, dash);
Jonas
  • 6,915
  • 8
  • 35
  • 53