Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2021-03-05 16:39:19


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 <boost/describe.hpp>

template<class T> struct X
{
        T m;

        BOOST_DESCRIBE_CLASS(X, (), (m), (), ())
};

int main()
{
        using namespace boost::describe;
        using D = describe_members<X<int>, mod_public>;
}

(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.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk