C++ macro/metaprogram to determine number of members at compile time

后端 未结 5 1536
鱼传尺愫
鱼传尺愫 2020-12-18 07:15

I am working on an application with a message based / asynchronous agent-like architecture. There will be a few dozen distinct message types, each represented by C++ types.<

相关标签:
5条回答
  • 2020-12-18 07:41

    as others already suggested, you need Boost.Fusion and its BOOST_FUSION_DEFINE_STRUCT. You'll need to define your struct once using unused but simple syntax. As result you receive required count_members (usually named as size) and much more flexibility than just that.

    Your examples:

    Definition:

    BOOST_FUSION_DEFINE_STRUCT(
        (), message_a,
        (long long, identifier),
        (double, some_value)
    )
    

    usage:

    message_a a;
    size_t count_members = message_a::size;
    
    0 讨论(0)
  • 2020-12-18 07:41

    Something like this might get you closer:

    struct Foo {
        Foo() : a(boost::get<0>(values)), b(boost::get<1>(values)) {}
        int &a;
        float &b;
        typedef boost::tuple<int,float> values_t;
        values_t values;
    };
    
    0 讨论(0)
  • 2020-12-18 07:43

    No, there is no way in C++ to know the names of all members or how many members are actually there.

    You could store all types in a mpl::vector along in your classes but then you face the problem of how to turn them into members with appropriate names (which you cannot achieve without some macro hackery).

    Using std::tuple instead of PODs is a solution that generally works but makes for incredible messy code when you actually work with the tuple (no named variables) unless you convert it at some point or have a wrapper that forwards accessors onto the tuple member.

    class message {
    public:
      // ctors
      const int& foo() const { return std::get<0>(data); }
      // continue boiler plate with const overloads etc
    
      static std::size_t nun_members() { return std::tuple_size<data>::value; }
    private:
      std::tuple<int, long long, foo> data;
    };
    

    A solution with Boost.PP and MPL:

    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/at.hpp>
    #include <boost/preprocessor.hpp>
    #include <boost/preprocessor/arithmetic/inc.hpp>
    
    struct Foo {
      typedef boost::mpl::vector<int, double, long long> types;
    
    // corresponding type names here
    #define SEQ (foo)(bar)(baz)
    #define MACRO(r, data, i, elem) boost::mpl::at< types, boost::mpl::int_<i> >::type elem;
    BOOST_PP_SEQ_FOR_EACH_I(MACRO, 0, SEQ)
    
    };
    
    int main() {
      Foo a;
      a.foo;
    }
    

    I didn't test it so there could be bugs.

    0 讨论(0)
  • 2020-12-18 07:44

    Plain structs do not support counting members, but boost::fusion offers a good way to declare a struct that is count- and iteratable.

    0 讨论(0)
  • 2020-12-18 07:46

    There are several answers simply saying that it is not possible, and if you hadn't linked to magic_get I would've agreed with them. But magic_get shows, to my amazement, that it actually is possible in some cases. This goes to show that proving that something is not possible is harder than proving that something is possible!

    The short answer to your question would be to use the facilities in magic_get directly rather than reimplement them yourself. After all, even looking at the pre-Boost version of the code, it's not exactly clear how it works. At one point in the comments it mentions something about constructor arguments; I suspect this is the key, because it is possible to count the arguments to a regular function, so perhaps it is counting the number of arguments needed to brace-initialise the struct. This indicates that it may only be possible with plain old structs rather than objects with your own methods.

    Despite all this, I would suggest using a reflection library as others have suggested. A good one that I often recommend is Google's protobuf library, which has reflection and serialisation along with multi-language support. However, it is intended only for data-only objects (like plain old structs but with vectors and strings).

    0 讨论(0)
提交回复
热议问题