Home > Backend Development > C++ > body text

Can You Dynamically Traverse Struct and Class Members in C ?

Susan Sarandon
Release: 2024-10-30 14:11:03
Original
937 people have browsed it

  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>
Copy after login

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>
Copy after login

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 &amp; x)
{
    visit_each(x, print_visitor());
}

A x;
print_fields(x);</code>
Copy after login

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>
Copy after login

Print the fields using this approach:

<code class="cpp">struct print_visitor
{
    template<class Index, class C>
    void operator()(Index, C &amp; 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 &amp; 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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!