
On Sat, Apr 3, 2010 at 7:22 PM, Ravi
Hi, How can karma be used to show the result of applying a member function to objects in a container? Consider the following code (which does not compile) to show the real and the imaginary parts of complex numbers stored in a vector:
--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- #include
#include #include #include #include <complex> #include <iostream> #include <vector>
namespace test {
template <typename Container> void show_container( const Container &ctr ) { typedef typename Container::value_type complex_t; typedef typename complex_t::value_type real_t; const real_t& ( complex_t::*real_func )() const = &complex_t::real; const real_t& ( complex_t::*imag_func )() const = &complex_t::imag;
namespace karma = boost::spirit::karma; namespace phoenix = boost::phoenix;
std::cout << karma::format( ( karma::double_[ // What goes here to extract the real part? karma::_1 = phoenix::bind( real_func, karma::_val ) ] << ',' << karma::double_[ karma::_1 = phoenix::bind( imag_func, karma::_val ) ] ) % karma::eol, ctr ); }
} // namespace test
int main( int, char *[] ) { std::vector< std::complex<double> > vec( 5 ); test::show_container( vec ); return 0; } --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--
The issue is that "karma::_1 = phoenix::bind( real_func, karma::_val )" seems unacceptable to the compiler (gcc 4.4.3, boost 1.42, Linux x86_64). The example above is a simplified version of much more complex code.
I would think that would work, but cannot test right now. You can always try to use the transform view in Fusion, that should work too, and execute slightly faster.