Boost logo

Boost :

Subject: [boost] DRYing BOOST_FUSION_ADAPT_STRUCT with decltype/Boost.TypeOf ?
From: Damien Buhl (damien.buhl_at_[hidden])
Date: 2013-12-17 20:35:37


Hi dear Boost Fusion developers,

I was asking myself if there were already plan to improve BOOST_FUSION_ADAPT_STRUCT to benefit from C++11 features like decltype ? This would be interesting to avoid the risk encountered that the types definitions can get out-of-sync. And would avoid typing type declarations twice.

Currently there is BOOST_FUSION_DEFINE_STRUCT to avoid repeating the type information for each field. However this is something that makes most developer I know unhappy, as it obliges to modify the readable C++ aggregate types definition (i.e. Could be public headers) with this macro.
 
Then there is the nice choice of BOOST_FUSION_ADAPT_STRUCT which obliges retyping the type and adds the risk that some field type information could get out-of-sync between the struct definition and the BOOST_FUSION_ADAPT_STRUCT listing.

I think it would be valuable to add macros that would enable to select the fields that we want to adapt to a Random Access Sequence without requiring to repeat the type information. I would find more comfortable to write the example provided here http://www.boost.org/doc/libs/1_55_0/libs/fusion/doc/html/fusion/adapted/adapt_struct.html as follow :

namespace demo
{
    struct employee
    {
        std::string name;
        int age;
    };
}

// demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_STRUCT_EXT(
    demo::employee,
    (name)
    (age))

And a naïve implementation of BOOST_FUSION_ADAPT_STRUCT_EXT could be:

#include <boost/typeof/typeof.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/preprocessor/seq/for_each.hpp>

#define ATTRIBUTE_DEFINITION(r, data, elem) \
    (BOOST_TYPEOF(data::elem), elem) \

#define BOOST_FUSION_ADAPT_STRUCT_EXT(NAME, ATTRIBUTES) \
    BOOST_FUSION_ADAPT_STRUCT(NAME, \
        BOOST_PP_SEQ_FOR_EACH(ATTRIBUTE_DEFINITION, NAME, ATTRIBUTES))

#endif

The same could certainly be achieved for BOOST_FUSION_ADAPT_TPL_STRUCT, while I didn't check.

Thanks for the really great job done in Boost.Fusion.

Cheers,

--
Damien Buhl
alias daminetreg

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