Home > Article > Backend Development > Can You Dynamically Traverse Struct and Class Members in C ?
Iterative Exploration of Struct and Class Members
In C , is it feasible to traverse a struct or class and discover all its members? Consider the following examples:
<code class="cpp">struct a { int a; int b; int c; }; class b { public: int a; int b; private: int c; };</code>
Is it possible to iterate through these structures and print statements such as "Struct a has int named a, b, c" or "Class b has int named a, b, c"?
Solution
To achieve this, you can utilize macros or adapt the struct as a fusion sequence.
Method 1: REFLECTABLE Macro
Define the struct using the REFLECTABLE macro as seen below:
<code class="cpp">struct A { REFLECTABLE ( (int) a, (int) b, (int) c ) };</code>
Subsequently, iterate over the fields and print each value:
<code class="cpp">struct print_visitor { template<class FieldData> void operator()(FieldData f) { std::cout << f.name() << "=" << f.get() << std::endl; } }; template<class T> void print_fields(T & x) { visit_each(x, print_visitor()); } A x; print_fields(x);</code>
Method 2: Fusion Sequence Adaptation
Adapt the struct as a fusion sequence:
<code class="cpp">struct A { int a; int b; int c; }; BOOST_FUSION_ADAPT_STRUCT ( A, (int, a) (int, b) (int, c) )</code>
Print the fields using this approach:
<code class="cpp">struct print_visitor { template<class Index, class C> void operator()(Index, C & c) { std::cout << boost::fusion::extension::struct_member_name<C, Index::value>::call() << "=" << boost:::fusion::at<Index>(c) << std::endl; } }; template<class C> void print_fields(C & c) { typedef boost::mpl::range_c<int,0, boost::fusion::result_of::size<C>::type::value> range; boost::mpl::for_each<range>(boost::bind<void>(print_visitor(), boost::ref(c), _1)); }</code>
The above is the detailed content of Can You Dynamically Traverse Struct and Class Members in C ?. For more information, please follow other related articles on the PHP Chinese website!