Code:
#include <iostream>
#include <cstdio>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/preprocessor/list/at.hpp>
typedef struct {
int s1;
int s2;
// ...
int s3;
} S;
// constructs "<struct>.<fieldname><number> = <value>;"
#define INIT_EACH_MEMBER_STATIC_INT(NextDim, Number, Data) \
BOOST_PP_LIST_AT(Data, 0).BOOST_PP_CAT(BOOST_PP_LIST_AT(Data, 1), Number) = BOOST_PP_LIST_AT(Data, 2);
// loops from startElem to startElem+elements constructing the assignment for each with the above macro
#define INIT_EACH_MEMBER_STATIC(structure, namePrefix, startElem, elements, value) \
BOOST_PP_REPEAT_FROM_TO(startElem, BOOST_PP_ADD(startElem, elements), INIT_EACH_MEMBER_STATIC_INT, (structure, (namePrefix, (value, BOOST_PP_LIST_NIL))))
// like the first macro, constructs
// <struct>.<fieldname><number> = <varprefix><value>;
#define INIT_EACH_MEMBER_VARIABLE_INT(NextDim, Number, Data) \
BOOST_PP_LIST_AT(Data, 0).BOOST_PP_CAT(BOOST_PP_LIST_AT(Data, 1), Number) = BOOST_PP_CAT(BOOST_PP_LIST_AT(Data, 2), Number);
#define INIT_EACH_MEMBER_VARIABLE(structure, namePrefix, startElem, elements, varPrefix) \
BOOST_PP_REPEAT_FROM_TO(startElem, BOOST_PP_ADD(startElem, elements), INIT_EACH_MEMBER_VARIABLE_INT, (structure, (namePrefix, (varPrefix, BOOST_PP_LIST_NIL))))
int main()
{
S myStructVar;
// make every member equal 56
INIT_EACH_MEMBER_STATIC(myStructVar, s, 1, 3, 56);
printf("S members: s1 = %d, s2 = %d, s3 = %d\n", myStructVar.s1, myStructVar.s2, myStructVar.s3);
// you could also use a macro for this to auto generate init variables
// and input for N values
int initS1, initS2, initS3;
std::cout << "Enter three ints: ";
std::cin >> initS1 >> initS2 >> initS3;
// make each member equal its eqivalent initS
INIT_EACH_MEMBER_VARIABLE(myStructVar, s, 1, 3, initS);
printf("S members: s1 = %d, s2 = %d, s3 = %d\n", myStructVar.s1, myStructVar.s2, myStructVar.s3);
}
So it can be done, but it is has a limited applicability given the stringent requirements on naming / initialization etc and the amount of manual bookkeeping you have to contend with. Use an array or vector for a much easier life.