|
Boost Users : |
Subject: Re: [Boost-users] [spirit][karma+phoenix] Show result of member function
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-03 22:05:40
On Sat, Apr 3, 2010 at 7:22 PM, Ravi <lists_ravi_at_[hidden]> wrote:
> 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 <boost/spirit/include/karma.hpp>
> #include <boost/spirit/include/phoenix_bind.hpp>
> #include <boost/spirit/include/phoenix_core.hpp>
> #include <boost/spirit/include/phoenix_operator.hpp>
>
> #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.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net