Boost logo

Boost Users :

From: JOAQUIN M. LOPEZ MUÑOZ (joaquin_at_[hidden])
Date: 2008-07-11 10:58:41


________________________________________
De: boost-users-bounces_at_[hidden] [boost-users-bounces_at_[hidden]] En nombre de vicente.botet [vicente.botet_at_[hidden]]
Enviado el: viernes, 11 de julio de 2008 10:56
Para: boost-users_at_[hidden]
Asunto: Re: [Boost-users] [preporcessor]Can we use BOOST_PP_SEQ_FOLD_LEFT on sequences with one element?

[...]
> Thanks Joaquin,
>
> the question now is how to treat sequences of size 1 separately from the
> general case.
> I have tried with BOOST_PP_IF, but it is no better.
[...]
> #define XX_SEQ_TO_QNAME(S) BOOST_PP_IF(XX_SIZE_GREATER_THAN_1(S)\
> , XX_SEQ_TO_QNAME_N(S) \
> , XX_SEQ_TO_QNAME_1(S))

The problem lies in the invocation to BOOST_PP_IF: you're evaluating
both XX_SEQ_TO_QNAME_N(S) and XX_SEQ_TO_QNAME_1(S),
which is precisely what you're trying to avoid. This is akin to doing the
following in runtime C++:

  int if_(bool c,int then_,int else_)
  {
    if(c)return then_;
    else return else_;
  }

  int safe_div(int n,int d)
  {
    return if_(d!=0,n/d,0);
  }

  cout<<safe_div(5,0);

You see why this fails? The same is happening to you in the PP realm. The
solution is to be lazy when it comes to invoking SEQ_TO_QNAME_*:

  #define XX_SEQ_TO_QNAME(S) BOOST_PP_IF(XX_SIZE_GREATER_THAN_1(S)\
      , XX_SEQ_TO_QNAME_N \
     , XX_SEQ_TO_QNAME_1)(S)

Hope this helps,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net