Boost logo

Boost Users :

Subject: [Boost-users] [proto] Problem using BOOST_PROTO_AUTO
From: Manjunath Kudlur (keveman_at_[hidden])
Date: 2010-02-22 02:12:59


I have the following program to print out the arguments of a function
call expression. It works fine when I directly call the grammar on the
expression like so : foo_tostr()(f(a,b,c)). However, if I assign the
expression to a variable pr using BOOST_PROTO_AUTO and call
foo_tostr()(pr), I get error of the form "conversion from
‘boost::fusion::... to non-scalar type .... requested". Can someone
let me know what I am doing wrong?

#include <iostream>
#include <boost/proto/proto.hpp>
#include <boost/proto/proto_typeof.hpp>
#include <sstream>
#include <string>

using namespace std;
namespace proto = boost::proto;

unsigned ids;

struct var {
  unsigned id;
  var() {
    id = ++ids;
  }
};

struct foo {};

typedef proto::terminal<var>::type Var;
typedef proto::terminal<foo>::type Foo;

struct _var_tostr
  : proto::callable {
  typedef string result_type;

  result_type operator()(var const &v) const {
    stringstream sout;
    sout << "v" << v.id << " ";
    return sout.str();
  }
  result_type operator()(foo const &v) const {
    stringstream sout;
    sout << "foo(";
    return sout.str();
  }

};

struct var_tostr
  : proto::or_<
proto::when<proto::terminal<var>, _var_tostr(proto::_value)>
> {};

struct str_plus : std::plus<std::string>, proto::callable {};

struct foo_tostr
  : proto::or_ <
proto::when<proto::function<proto::terminal<foo>,
            proto::vararg<proto::terminal<var> > >,
proto::fold<proto::functional::pop_front(proto::_expr), string(),
str_plus(proto::_state, var_tostr)> > > {};

int main()
{
  Var a, b, c;
  Foo f;

#define COPY_AND_PRINT
#ifdef COPY_AND_PRINT
  BOOST_PROTO_AUTO(pr, (f(a,b,c)));
  cout << foo_tostr()(pr) << endl;
#else
  cout << foo_tostr()(f(a,b,c)) << endl;
#endif
}

--
Manjunath

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