Home > Backend Development > C++ > body text

How can I iterate over struct and class members in C to access their names and values at runtime?

Mary-Kate Olsen
Release: 2024-10-28 03:02:31
Original
669 people have browsed it

How can I iterate over struct and class members in C   to access their names and values at runtime?

Iterating Over Struct and Class Members

In C , it is possible to iterate through the members of a struct or class to retrieve their names and values. Here are a few approaches to achieve this:

Using Macros

The REFLECTABLE macro can be used to define structs that allow for introspection. The macro defines the members of the struct as a comma-separated list of type-name pairs. For example:

<code class="cpp">struct A
{
    REFLECTABLE
    (
        (int) a,
        (int) b,
        (int) c
    )
};</code>
Copy after login

Once the struct is defined, you can use a visitor function to iterate over its members and print their names and values:

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

Adapting Structs as Fusion Sequences

Another approach is to adapt the struct as a fusion sequence using the BOOST_FUSION_ADAPT_STRUCT macro. This macro defines the struct members as a sequence of elements with the corresponding type and value. For example:

<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

Once the struct is adapted, you can use a range loop to iterate over the members and print their names and values:

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

Both these approaches allow you to introspect structs and classes, providing access to their members and their values at runtime.

The above is the detailed content of How can I iterate over struct and class members in C to access their names and values at runtime?. 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!