-1

I have data file which i want to load during preprocessing .

DATAFILE :
CAR(C1, C2, C3)

There can be n number of cars (C1, C2....Cn), currently 3. The C1,.. are enums fields with specific value say C1=5, C2-8, c3-10.

I want to populate this data into a car array CAR_SUPPORTED[MAX_CARS] such that

CAR_SUPPORTED[C1] = 1 and similarly for C2,C3.. so on.

I tried variadic macro as :

int CAR_SUPPORTED[] ={
#define  CAR(...) __VA_ARGS__};
#include "car.data"

But this could just copy 5, 8 , 10 to 0,1,2 indexes .

how would I write a macro such that CAR_SUPPORTED[C1] = 1 and so on . Any suggestions ?

CodeTry
  • 312
  • 1
  • 19
  • `int CAR_SUPPORTED[MAX_CARS] ={` ==> `int CAR_SUPPORTED[] ={` *i.e.* let the compiler count the number of elements. – pmg Oct 25 '18 at 11:45
  • why is this question given -1 ? isn't it asking for a valid programming logic ? – CodeTry Oct 26 '18 at 05:16

1 Answers1

0

Just use array initialization with a designator:

#define CAR(C1, C2, C3) [C1] = 1, [C2] = 1, [C3] = 1 };

If you which to use that for variadic number of args, I wold use P99 or boost preprocessor macros, or you can write macro expansion yourself. Grab example using boost:

#include <boost/preprocessor.hpp>

#define CAR_ONE(r, data, elem)     [elem] = 1,
#define CAR(...) BOOST_PP_SEQ_FOR_EACH(CAR_ONE,,BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) };

CAR(A1, A2, A3)
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I tried to look up the implementation in boost ,it is a web of Marcos .kind of got lost :( – CodeTry Oct 25 '18 at 16:44
  • It's the same ex examples [here](https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments) but bigger and more. No need to understand it, tho. And P99 is simpler. – KamilCuk Oct 25 '18 at 16:46