Search code examples
c++boostc-preprocessorboost-preprocessor

Rationale on Boost.Preprocessor using macros instead of simple defines?


For example BOOST_PP_ITERATE and BOOST_PP_ITERATION, as seen on GMan's answere here, are preprocessor macros, without any parameters. Is there a reason they're not just simple defines and used as such without ()?


Solution

  • Generally, function like macro can be used to prevent unintentional macro expansion.
    For example, assuming that we have the following macro call:

    BOOST_PP_CAT( BOOST_PP_ITERATION, _DEPTH )
    

    and we expect this will be expanded into BOOST_PP_ITERATION_DEPTH.
    However, if BOOST_PP_ITERATION is an object like(non-functional) macro, it will be expanded to its own definition before the token BOOST_PP_ITERATION_DEPTH is generated by concatenation.