Boost logo

Boost Users :

Subject: [Boost-users] [spirit][karma+phoenix] Show result of member function
From: Ravi (lists_ravi_at_[hidden])
Date: 2010-04-03 21:22:29


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.

Regards,
Ravi


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