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? };
The only way I can think of is either by just using a pointer, which I presume you don't want, or using a template alias, which would be something like:
template<size_t N>
using floatArr = float[N];
And when defining the struct
:
struct Test {
BOOST_HANA_DEFINE_STRUCT(Test,
(floatArr<2>, example)
);
};
An example program that illustrates this working: [GCC]
In fact, you could even generalize this alias for any type as well:
template<typename T, size_t N>
using CArray = T[N];
Now a C Style Array of any type can be used with BOOST_HANA_DEFINE_STRUCT
using a syntax such as CArray<float, 2> example
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With