Boost logo

Boost :

Subject: Re: [boost] [preprocessor] Warning: Incoming
From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2011-06-28 04:11:55


On Mon, 27 Jun 2011 23:25:15 +0200, Mathias Gaunard wrote:

> Consider something like
>
> BOOST_FOREACH(std::pair<A, B> a, mymap)
>
> this fails because the preprocessor sees it as three arguments.
>
> Assuming BOOST_FOREACH was using STRIP_PARENS on its arguments, you
> could do

Chaos has a macro that does this called CHAOS_PP_DECODE (and a
corresponding CHAOS_PP_ENCODE which is provided for symmetry).

However, assuming BOOST_FOREACH doesn't use it, you can still do it...

  // 1.cpp
  #include <boost/foreach.hpp>

  #include <chaos/preprocessor/facilities/type.h>
  #include <chaos/preprocessor/recursion/rail.h>

  CHAOS_PP_WALL(
      BOOST_FOREACH(
          CHAOS_PP_TYPE(std::pair<A, B> a),
          mymap
      )
  ) { /* ... */ }

g++ -E -P -std=gnu++0x -I $BOOST_ROOT -I $CHAOS_ROOT -D
CHAOS_PP_VARIADICS 1.cpp

...or, cleaned up...

  // 2.cpp
  #include <boost/foreach.hpp>

  #include <chaos/preprocessor/facilities/encode.h>
  #include <chaos/preprocessor/facilities/type.h>
  #include <chaos/preprocessor/recursion/rail.h>

  #define FOREACH(d, c) \
      CHAOS_PP_WALL(BOOST_FOREACH( \
          CHAOS_PP_TYPE(CHAOS_PP_DECODE(d)), \
          c \
      )) \
      /**/

  FOREACH((std::pair<A, B> a), mymap) { /* ... */ }

g++ -E -P -std=gnu++0x -I $BOOST_ROOT -I $CHAOS_ROOT -D
CHAOS_PP_VARIADICS 2.cpp

How's that for black magic?

Chaos calls this sort of thing a "rail" (in the railgun sense) because it
sends nearly arbitrary stuff through any amount of macro machinery and
expands it (and any number of copies of it) after it comes out the other
side regardless of where it appears in the output.

Regards,
Paul Mensonides


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