Boost.Phoenix で再帰処理

Boost.Phoenix再帰です。
書き方は、Boost.Lambda でのやり方と殆ど同じですね。
自分を呼び出すために予め自分を用意しておく必要があります。

[ソース]

#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/function.hpp>
#include <iostream>

int
main(){
    namespace phx = boost::phoenix;
    using phx::arg_names::arg1;
    
    boost::function<int(int)> factorial;
    factorial = phx::if_else(
        arg1 <= 0,
        phx::val(1),
        phx::bind(phx::ref(factorial), arg1 - 1) * arg1
    );
    
    std::cout << factorial(4) << std::endl;
    std::cout << factorial(10) << std::endl;
    return 0;
}

[出力]

24
3628800

[boost]

  • ver 1.47.0