Boost logo

Boost :

From: Larry Evans (cppljevans_at_[hidden])
Date: 2004-03-21 16:47:30


I've looked at some of the uses of these, but can't figure out how to do
the following. Using boost/preprocessor macros, I want to generate code
something like:

#include <boost/mpl/vector.hpp>
#include <boost/mpl/at.hpp>
using namespace boost::mpl;

struct source
{
       target
     my_target
     ;
     template<typename VecOfTypes>
     source
     ( VecOfTypes const&
     , at_c<VecOfTypes,0>::type a0
     )
     : my_target
       ( a0
       )
     {}
     template<typename VecOfTypes>
     source
     ( VecOfTypes const&
     , at_c<VecOfTypes,0>::type a0
     , at_c<VecOfTypes,1>::type a1
     )
     : my_target
       ( a0
       , a1
       )
     {}
};

and use it as follows:

     typedef vector<int,float> int_float_typelist;
     source s0(int_float_typelist(),0);
     source s1(int_float_typelist(),0,2.5);

I was planning on defining a macro, PP_CTOR_FORWARDER_OVERLOAD, to be
used as:

struct source
{
     BOOST_PP_REPEAT(2,PP_CTOR_FORWARDER_OVERLOAD,(source, my_target))
};

However, I also would like to use it where instead of my_target(a0,a1),
there might be super_type(new target(a0,a1)), in which case the tuple
that was the last arg to BOOST_PP_REPEAT would contain left and right
parens. However, when I tried:

BOOST_PP_REPEAT( 2, PP_CTOR_FORWARDER_OVERLOAD, (source,auto_ptr<target>
BOOST_PP_LPAREN() new target, BOOST_PP_RPAREN() ))

I get:

/usr/local/gcc-3.4-20040225/bin/g++ -ggdb -c -Wall -MMD -O0 -ggdb -E
-I/home/evansl/prog_dev/boost-root.ln/boost_dev
-I/home/evansl/prog_dev/boost-root.ln pp_ctor_forwarder.cpp |indent -st
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_0" requires 4
arguments, but only 2 given
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_1" requires 4
arguments, but only 2 given
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_2" requires 4
arguments, but only 2 given
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_0" requires 4
arguments, but only 2 given
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_1" requires 4
arguments, but only 2 given
pp_ctor_forwarder.cpp:4:1: macro "BOOST_PP_TUPLE_ELEM_4_2" requires 4
arguments, but only 2 given
# 1 "pp_ctor_forwarder.cpp"

How can I cause the delay in evaluation of BOOST_PP_RPAREN and LPAREN to
avoid these errors? Here is my current macro def:

#include "boost/preprocessor/enum_params.hpp"
#include "boost/preprocessor/inc.hpp"
#include "boost/preprocessor/punctuation/comma_if.hpp"
#include "boost/preprocessor/tuple/elem.hpp"
#define PP_CTOR_FORWARDER_PARAM(z,n,_) \
   BOOST_PP_COMMA_IF(n) at_c<VecOfTypes,n>::type BOOST_PP_CAT(a,n)

#define PP_CTOR_FORWARDER_OVERLOAD(z,n,data) \
   template< typename VecOfTypes > \
   BOOST_PP_TUPLE_ELEM(4,0,data) \
     ( VecOfTypes const& \
     , BOOST_PP_REPEAT(BOOST_PP_INC(n), PP_CTOR_FORWARDER_PARAM,
BOOST_PP_EMPTY) \
     ) \
     : BOOST_PP_TUPLE_ELEM(4,1,data) \
         BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(n), a ) \
       BOOST_PP_TUPLE_ELEM(4,2,data) \
   {}

TIA.


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