Question

Is there anyways to fusion::for_each() to iterate through a1 and a2 in a BOOST_FUSION_ADAPT_ADT or BOOST_FUSION_ADAPT_ASSOC_ADT, just like if adapted using BOOST_FUSION_ADAPT_STRUCT?

class A
{
private:
    int a1_;
    double a2_;

public:
    void set_a1(int v) { a1_ = v; }
    int get_a1() const { return a1_; }

    void set_a2(double v) { a2_ = v; }
    double get_a2() const { return a2_; }
};

BOOST_FUSION_ADAPT_ASSOC_ADT(
    A,
    (int, int, obj.get_a1(), obj.set_a1(val) )
    (double, double, obj.get_a2(), obj.set_a2(val) )
)

struct Print 
{
    template <typename T>
    void operator()( T& t ) const
    {
        // T is of type adt_attribute_proxy
        // cout << ??
        // would like to print a1 and a2 value
    }
};

int main()
{
    A a;
    boost::fusion::for_each( a, Print() );
}
Was it helpful?

Solution

adt_attribute_proxy provides method get to access attribute value.

struct Print
{

    template <typename T>
    void operator()(T& t) const
    {
        std::cout << t.get();
    }
};

P.S. There are errors in you sample BOOST_FUSION_ADAPT_ASSOC_ADT macro. Each element should be declared with 5 params (attribute_typeN, attribute_const_typeN, get_exprN, set_exprN, key_typeN) Maybe you mix up BOOST_FUSION_ADAPT_ASSOC_ADT with BOOST_FUSION_ADAPT_ADT?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top