简体   繁体   中英

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!

有 boost/hana/detail/struct_macros.hpp.erb 可用于重新生成 boost/hana/detail/struct_macros.hpp 使用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM