I'd like to build on the Boost Proto "Unpacking Expressions" example from here by using a template parameter to specify the return type of the do_eval
transform (hitherto double
).
For brevity I'll present a working, simplified (plus-only) version of do_eval
:
struct do_eval2 : proto::callable
{
typedef double result_type;
template <typename X, typename Y>
result_type operator()(proto::tag::plus, X x, Y y) const { return x + y; }
};
I then add a template parameter T
instead of double
:
template <typename T>
struct do_eval2 : proto::callable
{
typedef T result_type;
template <typename X, typename Y>
result_type operator()(proto::tag::plus, X x, Y y) const { return x + y; }
};
and modify the associated eval
structure to:
struct eval2
: proto::or_<
proto::when<proto::terminal<proto::_>, proto::_value>
, proto::otherwise<do_eval2<double>(proto::tag_of<proto::_>(),
eval2(proto::pack(proto::_))...)>
>
{};
but when I use it, as shown in the code below, I get errors starting with error: cannot bind ‘std::ostream {aka std::basic_ostream}’ lvalue to ‘std::basic_ostream&&’ How can I satisfy the compiler?
int main(int argc, char *argv[])
{
int one = 1, two = 2;
cout << eval2()(phoenix::ref(one)+phoenix::ref(two)) << '\n';
return 0;
}
As you can see here:
The documentation proposes to solutions: you either wrap every invocation of
do_eval<double>
withproto::call
or you simply specializeis_callable
inside the boost::proto namespace and forget about the problem.[Edit:] Here is the
proto::call
alternative: