1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <iostream>
#define DECL_MEMBER(r, data, elem) \
BOOST_PP_TUPLE_ELEM(3, 0, elem) BOOST_PP_TUPLE_ELEM(3, 1, elem);
#define INIT_MEMBER(r, data, elem) \
BOOST_PP_TUPLE_ELEM(3, 1, elem) = BOOST_PP_TUPLE_ELEM(3, 2, elem);
struct MyClass
{
#define MEMBER_LIST \
\
((int, mem1, 1)) \
((int, mem2, 2)) \
((double, mem3, 5.5)) \
((char, mem4, 'c'))
BOOST_PP_SEQ_FOR_EACH(DECL_MEMBER, ~, MEMBER_LIST)
MyClass()
{
BOOST_PP_SEQ_FOR_EACH(INIT_MEMBER, ~, MEMBER_LIST)
}
void Print() const
{
#define PRINT_MEMBER(r, data, elem) \
std::cout \
<< BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(3, 1, elem)) " = " \
<< BOOST_PP_TUPLE_ELEM(3, 1, elem) << std::endl;
BOOST_PP_SEQ_FOR_EACH(PRINT_MEMBER, ~, MEMBER_LIST)
#undef PRINT_MEMBER
}
#undef MEMBER_LIST
};
int main()
{
MyClass my_object;
my_object.Print();
return 0;
}
|
mem1 = 1
mem2 = 2
mem3 = 5.5
mem4 = c |