//////////////////////////////////////////////////////////////////////// // Program to test the combined usage of Boost.Units, Boost.Lambda, // and Boost.Function //////////////////////////////////////////////////////////////////////// // File: units_with_lambda_test.cpp // Author: Torsten Maehne // Created: 2008-05-27 //////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include // This new header maps Boost::Units' typeof helper trait classes to // the corresponding Boost.Lambda trait classes. If the #include is // commented out, Boost.Lambda won't be able to determine the return // type of arithmetic subexpressions. #include "lambda.hpp" int main(int argc, char **argv) { using namespace std; using boost::function; using namespace boost::lambda; using boost::lambda::_1; using namespace boost::units; //////////////////////////////////////////////////////////////////////// // Mechanical example: linear accelerated movement //////////////////////////////////////////////////////////////////////// quantity a = 2.0 * si::meters_per_second_squared; quantity v = 1.0 * si::meters_per_second; quantity s0 = 0.5 * si::meter; // Displacement over time function (quantity) > s = 0.5 * var(a) * _1 * _1 + var(v) * _1 + var(s0); cout << "Linear accelerated movement:" << endl << "a = " << a << ", v = " << v << ", s0 = " << s0 << endl << "s(1.0 * si::second) = " << s(1.0 * si::second) << endl< (quantity) > i = iamp * bind(static_cast::type (*)(const quantity&)>(sin), 2.0 * M_PI * si::radian * f * _1) + i0; cout << "Oscillating current:" << endl << "iamp = " << iamp << ", f = " << f << ", i0 = " << i0 << endl << "i(1.25e-3 * si::second) = " << i(1.25e-3 * si::second) << endl << endl; //////////////////////////////////////////////////////////////////////// // Geometric example: area calculation for a square //////////////////////////////////////////////////////////////////////// const quantity l = 1.5 * si::meter; // Again an ugly static_cast is needed to bind pow<2> to the first // function argument. function (quantity) > A = bind(static_cast (*)(const quantity&)>(pow<2>), _1); cout << "Area of a square:" << endl << "A(" << l <<") = " << A(l) << endl; return 0; }