Boost pre-processor: accessing elements in a sequence of tuples

I am trying to write some pre-processor macros that generate some logging functionality. I currently have a working example that works with simple sequences (i.e. FUNC( (a)(b)(c)(d) )

)

#include <boost/preprocessor.hpp>
#include <iostream>
#include <string>

#define X_DEFINE_ELEMENT_DISPLAY( r, data, elem ) \
    std::cerr << "( " << BOOST_PP_STRINGIZE( elem ) << " : " << v.elem << " ) ";

#define DEFINE_DISPLAY_FUNC( type, params )                 \
    void display( const type& v )                           \
    {                                                       \
        std::cerr << BOOST_PP_STRINGIZE( type ) << "{ ";    \
        BOOST_PP_SEQ_FOR_EACH(                              \
                X_DEFINE_ELEMENT_DISPLAY,                   \
                type, params )                              \
        std::cerr << " }\n";                                \
    }

struct MyStruct
{
    int   p1_;
    std::string p2_;
    unsigned    p3_;
};

DEFINE_DISPLAY_FUNC( MyStruct, (p1_)(p2_)(p3_) )

int main( int, char** )
{
    MyStruct s{ 23, "hello", 5467u };

    display( s );
    return 0;
}

      

If I modify the code to use a sequence of tuples (similarly BOOST_FUSION_ADAPT_STRUCT

) then the macro fails:

DEFINE_DISPLAY_FUNC( MyStruct, (int, p1_)(std::string, p2_)(unsigned, p3_) )

      

I understand this is because the elements are now "tuples", which causes the macro to BOOST_PP_SEQ_FOR_EACH

fail to expand correctly.

I am new to the pre-processor metaprogram and I have no idea what I should be using to properly expand / extract the required data.

+3
c ++ macros boost boost-preprocessor


source to share


No one has answered this question yet

See similar questions:

2
How do I use the boost preprocessor to create accessories?

or similar:

five
How to use Boost :: regex.hpp library in C ++?
4
Avoid expanding macros when using forced preprocessor sequences
2
Plus operator in boost qi doesn't work as expected
1
Where is the construct piece for boost :: container :: map :: emplace ()?
1
Boost Spirit QI: Output Auto-rule on a tuple with a sequence inside an alternative function
1
Insert result of macros sizeof () offsetof () into const char array?
0
Boost MPL - declare a class for each type in sequence
0
std :: for_each with boost :: zip_iterator won't compile
0
Xcode 5.1.1 and Boost
0
How to use boost :: transform to change sequence using lambda in template construct?



All Articles
Loading...
X
Show
Funny
Dev
Pics