PrevUpHomeNext

Calc2

The Proto Calc2 example turns the expressions from Calc1 into callable objects. Using Boost.YAP you can do this in two ways.

You can just use lambdas to wrap the expressions:

#include <boost/yap/expression.hpp>

#include <iostream>


int main ()
{
    using namespace boost::yap::literals;

    auto expr_1 = 1_p + 2.0;

    auto expr_1_fn = [expr_1](auto &&... args) {
        return evaluate(expr_1, args...);
    };

    auto expr_2 = 1_p * 2_p;

    auto expr_2_fn = [expr_2](auto &&... args) {
        return evaluate(expr_2, args...);
    };

    auto expr_3 = (1_p - 2_p) / 2_p;

    auto expr_3_fn = [expr_3](auto &&... args) {
        return evaluate(expr_3, args...);
    };

    // Displays "5"
    std::cout << expr_1_fn(3.0) << std::endl;

    // Displays "6"
    std::cout << expr_2_fn(3.0, 2.0) << std::endl;

    // Displays "0.5"
    std::cout << expr_3_fn(3.0, 2.0) << std::endl;

    return 0;
}

Or you can use make_expression_function() to make a callable object from your expression:

#include <boost/yap/expression.hpp>

#include <iostream>


int main ()
{
    using namespace boost::yap::literals;

    // Displays "5"
    std::cout << make_expression_function(1_p + 2.0)(3.0) << std::endl;

    // Displays "6"
    std::cout << make_expression_function(1_p * 2_p)(3.0, 2.0) << std::endl;

    // Displays "0.5"
    std::cout << make_expression_function((1_p - 2_p) / 2_p)(3.0, 2.0) << std::endl;

    return 0;
}


PrevUpHomeNext