Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:25:05+00:00 2026-06-10T05:25:05+00:00

Two stackoverflow answers suggest the approach using fusion adapt_struct to iterate over struct fields.

  • 0

Two stackoverflow answers suggest the approach using fusion adapt_struct to iterate over struct fields. The approach looks nice. However, how do you iterate into a field which itself is a struct?

Following the previous answers, I come up with the code below. The problem is at the “#if 0” clause the code does not compile. As an alternative solution I created “decode()” function to take a void pointer to the target argument. That works, but loses the type information at compile time. Is there a better solution?

struct Foo_s { int i; };
BOOST_FUSION_ADAPT_STRUCT( Foo_s,  (int, i) )

struct Bar_s { int v; Foo_s w; };
BOOST_FUSION_ADAPT_STRUCT( Bar_s, (int, v)  (Foo_s, w) )

struct AppendToTextBox {
    template <typename T> void operator()(T& t) const {
        int status = 0;
        const char *realname = abi::__cxa_demangle(typeid(t).name(), 0, 0, &status);
        printf("  typename: %s  value: %s  realname: %s\n", typeid(t).name(),
               boost::lexical_cast<std::string>(t).c_str(), realname);
        std::string rn(realname);
        if ( rn.rfind("_s") == rn.size()-2 ) {
#if 0 /* this can not compile */
            for_each(t, AppendToTextBox());
#else
            decode(&t, rn);
#endif
        }
    }
};

void decode(void *f, std::string & intype ) {
    if ( intype.find("Foo_s") == 0 ) 
        for_each( *(Foo_s *)f, AppendToTextBox());
};

int main(int argc, char *argv[]) {
  Bar_s f = { 2, { 3 } };
  for_each(f, AppendToTextBox());
  return 0;
}

I have seen on wikipedia instead of passing a type string “intype” you can use typeid and dynamic_cast. But that will only be a minor improvement. I’m looking for a solution that is more intrinsic to C++ or boost language design.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:25:07+00:00Added an answer on June 10, 2026 at 5:25 am

    I made an example of what you want that you can see at my blog site. In this is case it’s a JSON serializer that works with nested structs. It uses a ‘more Boost’ solution since I saw it in the Boost.Serialization library. (See also below and live on Coliru.)

    The solution uses Fusion Sequence adaptation of structs and a metafunction that walks object members (recursively) – using Boost.TypeTraits and different traits for specific types.

    You can see a more complex example of the same solution at the site for googlecode corbasim project for creating an run-time reflexive API.

    Code listing for the generic JSON serializer:

    See it Live on Coliru

    #ifndef JSON_SERIALIZER_HPP
    #define JSON_SERIALIZER_HPP
    
    #include <boost/type_traits.hpp> // is_array, is_class, remove_bounds
    
    #include <boost/mpl/eval_if.hpp>
    #include <boost/mpl/identity.hpp>
    #include <boost/mpl/next_prior.hpp>
    
    #include <boost/fusion/mpl.hpp>
    #include <boost/fusion/adapted.hpp> // BOOST_FUSION_ADAPT_STRUCT
    
    // boost::fusion::result_of::value_at
    #include <boost/fusion/sequence/intrinsic/value_at.hpp>
    #include <boost/fusion/include/value_at.hpp>
    
    // boost::fusion::result_of::size
    #include <boost/fusion/sequence/intrinsic/size.hpp>
    #include <boost/fusion/include/size.hpp>
    
    // boost::fusion::at
    #include <boost/fusion/sequence/intrinsic/at.hpp>
    #include <boost/fusion/include/at.hpp>
    
    namespace json
    {
    
    // Forward
    template < typename T >
    struct serializer;
    
    namespace detail
    {
    
    namespace iterator
    {
    
    template < typename S, typename N >
    struct Comma
    {
        template < typename Ostream >
        static inline void comma(Ostream& os)
        {
            os << ", ";
        }
    };
    
    template < typename S >
    struct Comma< S, typename boost::mpl::prior< typename boost::fusion::result_of::size< S >::type >::type >
    {
        template < typename Ostream >
        static inline void comma(Ostream& os)
        {
        }
    };
    
    // Iteracion sobre una estructura
    template < typename S, typename N >
    struct StructImpl
    {
        // Tipo del campo actual
        typedef typename boost::fusion::result_of::value_at< S, N >::type current_t;
        typedef typename boost::mpl::next< N >::type next_t;
        typedef boost::fusion::extension::struct_member_name< S, N::value > name_t;
    
        template < typename Ostream >
        static inline void serialize(Ostream& os, const S& s)
        {
            os << "\"" << name_t::call() << "\": ";
            ::json::serializer< current_t >::serialize(os, boost::fusion::at< N >(s));
    
            // Insert comma or not    
            Comma< S, N >::comma(os);
    
            StructImpl< S, next_t >::serialize(os, s);
        }
    };
    
    // Fin de la iteracion sobre estructuras.
    template < typename S >
    struct StructImpl< S, typename boost::fusion::result_of::size< S >::type >
    {
        template < typename Ostream >
        static inline void serialize(Ostream& os, const S& s)
        {
            // Nada que hacer
        }
    };
    
    // Iterador sobre una estructura. Template fachada.
    template < typename S >
    struct Struct : StructImpl< S, boost::mpl::int_< 0 > > {};
    
    } // iterator
    
    template < typename T >
    struct array_serializer 
    {
        typedef array_serializer< T > type;
    
        typedef typename boost::remove_bounds< T >::type slice_t;
    
        static const size_t size = sizeof(T) / sizeof(slice_t);
    
        template < typename Ostream >
        static inline void serialize(Ostream& os, const T& t)
        {
            os << "[";
            for(size_t idx=0; idx<size; idx++)
            {
                ::json::serializer< slice_t >::serialize(os, t[idx]);
                if (idx != size-1)
                    os << ", ";
            }
            os << "]";
        }
    
    };
    
    template < typename T >
    struct struct_serializer 
    {
        typedef struct_serializer< T > type;
    
        template < typename Ostream >
        static inline void serialize(Ostream& os, const T& t)
        {
            os << "{";
            iterator::Struct< T >::serialize(os, t);
            os << "}";
        }
    };
    
    template < typename T >
    struct arithmetic_serializer 
    {
        typedef arithmetic_serializer< T > type;
    
        template < typename Ostream >
        static inline void serialize(Ostream& os, const T& t)
        {
            os << t;
        }
    };
    
    template < typename T >
    struct calculate_serializer
    {
        typedef
            typename boost::mpl::eval_if< boost::is_array< T >,
                boost::mpl::identity< array_serializer < T > >,
            //else
            typename boost::mpl::eval_if< boost::is_class< T >,
                boost::mpl::identity< struct_serializer < T > >,
            //else
                boost::mpl::identity< arithmetic_serializer < T > >
            >
            >::type type;
    
    };
    
    } // detail
    
    template < typename T >
    struct serializer : public detail::calculate_serializer < T >::type
    {
    };
    
    
    } // json
    
    #endif // JSON_SERIALIZER_HPP
    
    //#include "json.hpp"
    #include <iostream>
    
    struct my_other_struct
    {
        int my_other_integer;
    };
    
    struct my_struct
    {
        int my_integer;
    
        typedef int my_array_t[2];
        my_array_t my_array;
    
        typedef my_other_struct my_other_structs_t[3];
        my_other_structs_t my_other_structs;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(my_struct, (int, my_integer) (my_struct::my_array_t, my_array) (my_struct::my_other_structs_t, my_other_structs))
    BOOST_FUSION_ADAPT_STRUCT(my_other_struct, (int, my_other_integer))
    
    
    int main(int argc, char *argv[])
    {
        my_struct s1 = my_struct { 1, { 42, -42 }, { { 11 }, { 22 }, { 33 } } };
    
        json::serializer< my_struct >::serialize(std::cout, s1);
    
        std::cout << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen two questions relating to my topic on stackoverflow, but those answers
I spent around two hours on that problem, and I have visited these stackoverflow
I'm trying to diff two strings by phrase, similar to the way that StackOverflow
--Solution found-- The correct PHP code is a combination of the two answers provided
After reading some examples on stackoverflow, and following some of the answers for my
Hello I am using will_paginte plugin on two objects on a same page. Like
I couldn't found this answer in Stackoverflow, but I have two domains and two
First stackoverflow question! I've searched...I promise. I haven't found any answers to my predicament.
Thanks for the answers, I have not used StackOverflow before so I was suprised
Code below from other stackoverflow answer is used in jqGrid to implement checkbox using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.