Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2008-06-06 16:51:26


Daniel Walker wrote:
>
> OK, but I was thinking more of user defined function objects. Something like ...
>
> struct f_ {
> template<class> struct result {
> typedef void type;
> };
> template<class T>
> void operator()(T& t) { ++t; }
> };
>
> int main()
> {
> phoenix::function<f_> f;
> int i = 0;
> f(i);

That creates a lazy function invocation, but doesn't execute it.

> assert(i == 1);
> }
>
> Actually, I just tried this and the assertion failed. So, I assume
> user defined function objects are required to be "pure." Why is that?

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

namespace phoenix = boost::phoenix;

struct f_
{
     template<class> struct result {
         typedef void type;
     };
     template<class T>
     void operator()(T& t) { ++t; }
};

int main()
{
     using phoenix::arg_names::_1;
     phoenix::function<f_> f;

     int i = 0;
     f(_1)(i); // OK
     assert(i == 1);

     int j = 0;
     //f(j)(); // by value, doesn't compile
     //assert(j == 0);

     f(phoenix::ref(j))(); // by ref, OK
     assert(j == 1);
}

HTH,

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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