4

Suppose i have a structure or a similar class(C++) like this :

    struct str
    {
        int a = 5;
        char b = 'x';
    };

Is there a way to enumerate its members.I want to find the names of the members, the values and if possible the datatypes.

My purpose is to build a config file. So i basically have to represent command packet structures in some way. When the actual data comes i want to compare the packet with this config file and then process it. So there are different command types. So each time when a packet comes, i have to take its opcode and check it with the names of the structures and return the appropriate structure that represents the format of that packet.

My thinking is to represent the command packet format in structures. If you can recommend other data structure to hold this, then also it is ok. The languages can be C or C++. Performance is top priority so xml and similar types are discouraged. In memory data structures are preferred. Any help is highly appreciated. Thank you

melpomene
  • 84,125
  • 8
  • 85
  • 148
GustavoS
  • 87
  • 1
  • 9
  • does c have default initializers? Be carefull with tagging two different lanuages. It is really rare that questions/answers are the same for c and for c++ – 463035818_is_not_an_ai May 10 '19 at 09:51
  • 1
    Possible duplicate of [C/C++ Possible to get a "list" of instance members by querying a class?](https://stackoverflow.com/questions/4110037/c-c-possible-to-get-a-list-of-instance-members-by-querying-a-class) – nwp May 10 '19 at 09:52

2 Answers2

3

You can't do this in portable C or C++. Neither C nor C++ are reflective languages although reflection is on the list of things that may be available in a future C++ standard.

Some compilers when implementing "debug builds" will provide ways of your implementing what you want (consult the documentation) but any code you write will not be portable.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    There are some build systems (e.g. the Qt moc) that add reflection-like features and are somewhat portable. But those will likely add runtime overhead. – perivesta May 10 '19 at 09:51
3

Macro-based pseudo reflection with boost.hana:

#include <iostream>
#include <string>
#include <boost/hana.hpp>

struct Person
{
    BOOST_HANA_DEFINE_STRUCT
    (
        Person
    ,  (::std::string, name)
    ,  (int, age)
    );
};

int main()
{
    Person john{"John", 30};
    ::boost::hana::for_each
    (
        ::boost::hana::members(john)
    ,   [](auto const & member)
        {
            ::std::cout << member << ::std::endl;
        }
    );
    return 0;
}
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • How is the performance of the boost/hana ? Since speed is important i dont know if i can use this. – GustavoS May 10 '19 at 09:56
  • @GustavoS Hand written RTTI adopted to particular use case could probably be written to work faster than stock hana implementations. But I doubt that it would be a performance bottleneck. – user7860670 May 10 '19 at 10:03
  • I like this, and it possibly anticipates a future C++ standard (as Boost often does). Have an upvote. – Bathsheba May 10 '19 at 10:03