Search code examples
c++boostmetaprogrammingboost-hana

Define struct having more than 40 fields using BOOST_HANA_DEFINE_STRUCT in Boost::Hana


Defining a struct that has more than 40 fields using macro BOOST_HANA_DEFINE_STRUCT in library Boost::hana, for example,

#include <boost/hana/define_struct.hpp>
namespace hana = boost::hana;

// a simple struct having 41 fields
struct Foo {
    BOOST_HANA_DEFINE_STRUCT(Foo,
        (int, bar_0),
        (int, bar_1),
        ...,
        (int, bar_40)
    );
};

will raise an compile error

pasting "BOOST_HANA_DEFINE_STRUCT_IMPL_" and "(" does not give a valid preprocessing token.

Looking deeply into the source code of Hana, one can found that in file boost/hana/detail/struct_macros.hpp, all macros are defined with hard coded implements from a boilerplate,

...
#define BOOST_HANA_DEFINE_STRUCT_IMPL_40(TYPE , m1, m2, ..., m39) \
#define BOOST_HANA_DEFINE_STRUCT_IMPL_41(TYPE , m1, m2, m3, ..., m40) \

So my question is

  1. How can I define a new max argument number, such as 128, and regenerate the macros, so that more arguments are able to pass?

  2. Is it a good practice to use such a struct implemented by Hana tuple but having too many fields? If it is not a good design, what alternative is recommended?

Thanks very much!


Solution

  • There is boost/hana/detail/struct_macros.hpp.erb which can be used to regenerate boost/hana/detail/struct_macros.hpp using

    MAX_NUMBER_OF_MEMBERS=55 erb struct_macros.hpp.erb > struct_macros.hpp