Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting field names with boost::pfr

Hi I'm using boost::pfr for basic reflection, it works fine, but the problem is it is only print or deal with the field values, like with boost::pfr::io it prints each member of the struct, but how can I print it as name value pairs, same issue with for_each_field, the functor only accepts values, but not names. How can I get the field names?

struct S {
    int n;
    std::string name;
};
S o{1, "foo"};
std::cout << boost::pfr::io(o);
// Outputs: {1, "foo"}, how can I get n = 1, name = "foo"?
like image 716
fluter Avatar asked Dec 29 '25 13:12

fluter


1 Answers

If you think adapting a struct is not too intrusive (it doesn't change your existing definitions, and you don't even need to have it in a public header):

BOOST_FUSION_ADAPT_STRUCT(S, n, name)

Then you can concoct a general operator<< for sequences:

namespace BF = boost::fusion;

template <typename T,
          typename Enable = std::enable_if_t<
              // BF::traits::is_sequence<T>::type::value>
              std::is_same_v<BF::struct_tag, typename BF::traits::tag_of<T>::type>>>
std::ostream& operator<<(std::ostream& os, T const& v)
{
    bool first = true;
    auto visitor = [&]<size_t I>() {
        os << (std::exchange(first, false) ? "" : ", ")
           << BF::extension::struct_member_name<T, I>::call()
           << " = " << BF::at_c<I>(v);
    };

    // visit members
    [&]<size_t... II>(std::index_sequence<II...>)
    {
        return ((visitor.template operator()<II>(), ...);
    }
    (std::make_index_sequence<BF::result_of::size<T>::type::value>{});
    return os;
}

(Prior to c++20 this would require some explicit template types instead of the lambdas, perhaps making it more readable. I guess I'm lazy...)

Here's a live demo: Live On Compiler Explorer

n = 1, name = foo

Bonus: Correctly quoting string-like types

Live On Compiler Explorer

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <iostream>
#include <iomanip>

namespace MyLib {
    struct S {
        int         n;
        std::string name;
    };

    namespace BF = boost::fusion;

    static auto inline pretty(std::string_view sv) { return std::quoted(sv); }

    template <typename T,
            typename Enable = std::enable_if_t<
                not std::is_constructible_v<std::string_view, T const&>>>
    static inline T const& pretty(T const& v)
    {
        return v;
    }

    template <typename T,
            typename Enable = std::enable_if_t<
                // BF::traits::is_sequence<T>::type::value>
                std::is_same_v<BF::struct_tag, typename BF::traits::tag_of<T>::type>>>
    std::ostream& operator<<(std::ostream& os, T const& v)
    {
        bool first = true;
        auto visitor = [&]<size_t I>() {
            os << (std::exchange(first, false) ? "" : ", ")
            << BF::extension::struct_member_name<T, I>::call()
            << " = " << pretty(BF::at_c<I>(v));
        };

        // visit members
        [&]<size_t... II>(std::index_sequence<II...>)
        {
            return (visitor.template operator()<II>(), ...);
        }
        (std::make_index_sequence<BF::result_of::size<T>::type::value>{});
        return os;
    }
} // namespace MyLib

BOOST_FUSION_ADAPT_STRUCT(MyLib::S, n, name)

int main()
{
    MyLib::S o{1, "foo"};
    std::cout << o << "\n";
}

Outputs:

n = 1, name = "foo"
like image 169
sehe Avatar answered Jan 01 '26 01:01

sehe