dimanche 20 septembre 2015

Is it possible to introspect on methods using Boost Hana?

Boost Hana provides the ability to introspect on class member fields in a simple and beautiful way:

// define:

struct Person {
  std::string name;
  int age;
};

// below could be done inline, but I prefer not polluting the 
// declaration of the struct
BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person, name, age);

// then:

Person john{"John", 30};
hana::for_each(john, [](auto pair) {
  std::cout << hana::to<char const*>(hana::first(pair)) << ": "
            << hana::second(pair) << std::endl;
});

However, the documentation only mentions member fields. I would like to introspect on methods also. I've tried to naively extend the example with a method:

struct Foo {
    std::string get_name() const { return "louis"; }
};

BOOST_HANA_ADAPT_STRUCT(::Foo, get_name);

This compiles. However, as soon as I try to use it, using code similar to the above (for_each...), I get many compile errors. Since there are no examples that show introspection of methods, I'm wondering whether it is supported.





Aucun commentaire:

Enregistrer un commentaire