Boost logo

Boost :

Subject: [boost] [phoenix] Functions and function pointers
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-04-07 19:15:00


Hello all,

How do I call functions and function pointers from within a
Boost.Phoenix lambda function?

For example:

#include <boost/spirit/include/phoenix.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

#define N 1e4
#define S N * 1e2

const double& id(const double& x) { return x; }

int main() {
    using boost::phoenix::ref;
    using boost::phoenix::arg_names::_1;

    double sum = 0.0;
    int factor = 1;
    const double& (*f)(const double&) = &id;

    std::vector<double> v(S);
    std::fill(v.begin(), v.end(), 1.0);

    for (size_t n = 0; n < N; ++n) {
        std::for_each(v.begin(), v.end(), (
            ref(sum) += id(factor * f(_1)) // <<<<<<<<<<<<<< how do I
call id(...) and f(...)?
        ));
    }

    std::cout << sum << std::endl;
    assert(sum == N * S);
    return 0;
}

Thank you very much.

-- 
Lorenzo

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