lundi 5 février 2018

Reflecting C-style arrays with Boost Hana

BOOST_HANA_DEFINE_STRUCT is a fantastic macro when declaring structures with introspection. If I have a structure like this:

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

We can add introspection by redefining it like this:

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

But what if we have a structure like this:

struct Person {
  float eye_dioptre[2];
};

How would I use the BOOST_HANA_DEFINE_STRUCT syntax to reflect a C-style array? I've tried:

struct Person {
  BOOST_HANA_DEFINE_STRUCT(Person, 
    (float[2], eye_dioptre),   // error: expected unqualified-id before ‘[’ token
    (float, eye_dioptre[2])    // error: template argument 2 is invalid  BOOST_HANA_DEFINE_STRUCT(structure_name, __VA_ARGS__ );                    
  );
};

Both options above give compiler errors. The answer I'm expecting is "you should use c++-style arrays". That could be done like so:

struct Person {
  BOOST_HANA_DEFINE_STRUCT(Person,
    (std::array<float,2>, eye_dioptre)
  );

But is there a way to do it with C-style arrays? };





Aucun commentaire:

Enregistrer un commentaire