Boost logo

Boost Users :

Subject: Re: [Boost-users] [phoenix] Basic recursive function.
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-04-13 11:35:38


At Wed, 13 Apr 2011 16:10:12 +0100,
Robert Jones wrote:
>
> I'm a newbie to Phoenix, just exploring, but I guess my intent in this code
> is
> obvious. Can anyone tell me what I'm doing wrong?
>
> Thx
>
> - Rob.
>
> #include <iostream>
> #include <boost/spirit/include/phoenix_core.hpp>
> #include <boost/spirit/home/phoenix/operator.hpp>
> #include <boost/spirit/home/phoenix/statement.hpp>
> #include <boost/function.hpp>
>
> using namespace boost::phoenix;
> using namespace boost::phoenix::arg_names;
> using namespace std;
>
> int main()
> {
> boost::function<unsigned( unsigned )> factorial;
> factorial = if_else( _1 == 0, 1, _1 * factorial( _1 - 1 ) );

I think you'd better find a way to store factorial by reference in the
phoenix expression, because the value at the time of building is
empty. Totally guessing:

        factorial = if_else( _1 == 0, 1, _1 * ref(factorial)( _1 - 1 ) );

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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