Boost logo

Boost :

From: Vesa Karvonen (vesa_karvonen_at_[hidden])
Date: 2002-04-12 00:36:04


David Abrahams:
>How can I make a macro which, given an integer N, generates a sequence
>of N concatenated Xs?
>
>IOW,
>
> Xs(0) ->
> Xs(5) -> XXXXX
>
>...and so forth?

This can be done using BOOST_PP_WHILE. Try the following program:

<-- xs.cpp -->
#include <boost/preprocessor.hpp>

#define XS(N,X)\
  XS_D(0,N,X)
#define XS_D(D,N,X)\
  BOOST_PP_EXPR_IF\
  ( N\
  , BOOST_PP_TUPLE_ELEM\
    ( 3\
    , 2\
    , BOOST_PP_WHILE##D(XS_C,XS_F,(BOOST_PP_DEC(N),X,X))\
    )\
  )
#define XS_C(D,NXR)\
  BOOST_PP_TUPLE_ELEM(3,0,NXR)
#define XS_F(D,NXR)\
  ( BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(3,0,NXR))\
  , BOOST_PP_TUPLE_ELEM(3,1,NXR)\
  , BOOST_PP_CAT\
    ( BOOST_PP_TUPLE_ELEM(3,1,NXR)\
    , BOOST_PP_TUPLE_ELEM(3,2,NXR)\
    )\
  )

0: XS(0,X)
1: XS(1,X)
2: XS(2,X)
3: XS(3,X)
<-- xs.cpp -->

>Fortunately, in my case I need a string literal so I don't actually
>need to catenate them (I can rely on "X" "X" "X" to generate "XXX")
>but the problem still looks unreasonably hard-to-solve with BPPL.

This can be done using BOOST_PP_REPEAT (as you seem to have discovered).

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail.
http://www.hotmail.com


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