pt., 5 mar 2021 o 17:39 Peter Dimov via Boost
Julien Blanc wrote:
BOOST_DESCRIBE_STRUCT must be put outside the class definition. BOOST_DESCRIBE_CLASS, on the other hand, must be put inside the class definition. This is typically the "i will always get this wrong" case. Since the library heavily relies on templating and macros, the error messages are arcane to the final user. I would suggest, that, for consistency, BOOST_DESCRIBE_STRUCT should be put inside the class definition as well.
BOOST_DESCRIBE_CLASS goes inside the class definition for two reasons: one, to be able to access private members; two, to support class templates. You can do
#include
template<class T> struct X { T m;
BOOST_DESCRIBE_CLASS(X, (), (m), (), ()) };
int main() { using namespace boost::describe; using D = describe_members
; } (except it seems it doesn't work on MSVC 2017, which I currently use for unrelated reasons, but it does work on g++ and clang++ :-))
In contrast, BOOST_DESCRIBE_STRUCT being put outside the struct allows you to describe structs whose definitions you don't control, such as structs defined in third party libraries and system headers.
So there's a reason for both decisions.
It makes sense to support both cases. Maybe it is just the choice of names. In C++ we are taught that struct is actually a class. Maybe a rename BOOST_DESCRIBE_STRUCT -> BOOST_DESCRIBE_AGGREGATE (or similar) would make the difference easier to spot. Regards, &rzej;