
On Thu, Feb 9, 2012 at 5:46 PM, Bill Buklis
Is there any facility to perform arithmetic operations on a tuple?
For example:
struct data { double a, b; };
data d1 = {1.0, 2.0}; data d2 = {2.5, 5.5};
// These two lines are not valid, but simulate what I want. boost::tie(d1.a, d1.b) *= 10.0; // Multiply each element by 10.0 boost::tie(d1.a, d1.b) += boost::tie(d2.a, d2.b); // Add the elements from the second object
After calculations, d1 should equal {12.5, 25.5};
Boost.Fusion + Boost.Phoenix *might* be what you're looking for, but I don't know. Specifically, boost::fusion::for_each [1] lets you iterate over a tuple and apply a (polymorphic) function to each element; that function can be built inline using Boost.Phoenix. E.g., (untested) boost::fusion::for_each(d1, boost::phoenix::arg_names::_1 *= 10.0); Your second example above might be a bit more verbose using these techniques. Check out also boost::fusion::transform [2] and, maybe, boost::fusion::zip_view [3]? Also, you'll have to adapt [4] your data struct to be recognizable as a Boost.Fusion sequence. [1] http://www.boost.org/doc/libs/1_48_0/libs/fusion/doc/html/fusion/algorithm/i... [2] http://www.boost.org/doc/libs/1_48_0/libs/fusion/doc/html/fusion/algorithm/t... [3] http://www.boost.org/doc/libs/1_48_0/libs/fusion/doc/html/fusion/view/zip_vi... [4] http://www.boost.org/doc/libs/1_48_0/libs/fusion/doc/html/fusion/adapted/ada... - Jeff