Boost Phoenix Arguments Passing & Storage (-- Proper Type?) & Application to RValues -- Possible?

Hi there! I've got a question (two, in fact) regarding Arguments in Boost Phoenix. Assuming using boost::phoenix::arg_names::arg1; using boost::phoenix::val; using boost::phoenix::ref; using boost::phoenix::cref; Why does this compile (and works): int x = 1; int y = 2; std::cout << (arg1 % 2 == 1)(x) << '\n'; std::cout << (arg1 % 2 == 1)(y) << '\n'; while these fail to compile (with an extremely long diagnostic involving "boost::error_cant_deduce_type"): std::cout << (arg1 % 2 == 1)(1) << '\n'; std::cout << (arg1 % 2 == 1)(2) << '\n'; std::cout << (arg1 % 2 == 1)(val(1)) << '\n'; std::cout << (arg1 % 2 == 1)(val(2)) << '\n'; std::cout << (arg1 % 2 == 1)(cref(1)) << '\n'; std::cout << (arg1 % 2 == 1)(cref(2)) << '\n'; I run into this was while attempting to store an Arg. into a variable, i.e.,: auto f = (arg1 % 2 == 1); this works: std::cout << f(x) << '\n'; std::cout << f(y) << '\n'; while this fails: std::cout << f(1) << '\n'; std::cout << f(2) << '\n'; I'm guessing this must be for the same reason the above attempts fail. Another question is, what's the "proper" type of "f" assuming non-C++11 compiler? I'd like to avoid wrappers like boost::function (assuming that's even possible). Later on, I'd like to return such an "f" from a function, hence the question (in C++11 I can get away with decltype for short and easy ones, i.e., prototypes like this "auto newActor() -> decltype((arg1 % 2 == 1))" work, but this becomes inconvenient for longer bodies). A concoction like this compiles but doesn't produce an output: auto f = (val(arg1) % 2 == 1); std::cout << f(val(0)) << '\n'; // and I'm guessing this is very wrong anyway due to unusual behavior. Best, Matt

On 02/14/2012 10:43 PM, Matt wrote:
Hi there!
I've got a question (two, in fact) regarding Arguments in Boost Phoenix.
Assuming using boost::phoenix::arg_names::arg1; using boost::phoenix::val; using boost::phoenix::ref; using boost::phoenix::cref;
Why does this compile (and works): int x = 1; int y = 2; std::cout << (arg1 % 2 == 1)(x) << '\n'; std::cout << (arg1 % 2 == 1)(y) << '\n';
while these fail to compile (with an extremely long diagnostic involving "boost::error_cant_deduce_type"):
std::cout << (arg1 % 2 == 1)(1) << '\n'; std::cout << (arg1 % 2 == 1)(2) << '\n';
std::cout << (arg1 % 2 == 1)(val(1)) << '\n'; std::cout << (arg1 % 2 == 1)(val(2)) << '\n';
std::cout << (arg1 % 2 == 1)(cref(1)) << '\n'; std::cout << (arg1 % 2 == 1)(cref(2)) << '\n';
I run into this was while attempting to store an Arg. into a variable, i.e.,: auto f = (arg1 % 2 == 1); this works: std::cout << f(x) << '\n'; std::cout << f(y) << '\n'; while this fails: std::cout << f(1) << '\n'; std::cout << f(2) << '\n'; I'm guessing this must be for the same reason the above attempts fail.
You're using Phoenix v2, which doesn't support rvalues and also has many type deduction problems. Try v3.

On 2/15/2012 03:57, Mathias Gaunard wrote:
You're using Phoenix v2, which doesn't support rvalues and also has many type deduction problems. Try v3.
Thanks! Upgrading to Version 1.49.0 beta 1 fixed the Application-to-RValues part. Now, to the proper-type-name for passing & storage part. I've tried the following and it works: auto newActor() -> decltype((arg1 % 2 == 1)) { return (arg1 % n == 0); } auto f = (arg1 % 2 == 1); auto g = f; g = newActor(); I can find out the type name using non-portable typeid(Variable).name(), but the output is somewhat inconvenient (quoted below). Is there a nice-to-type solution? It'd be esp. useful for non-C++11 compiler, where I cannot use trailing-return-type for "newActor" function nor type deduction for "f" and "g" variables -- what's the suggested type in this situation? I've realized that using boost::function (or std::function) won't work either, since it's monomorphic (while one of the reasons I'm looking at Boost.Phoenix is polymorphic lambdas). Type name: struct boost::phoenix::actor < struct boost::proto::exprns_::basic_expr < struct boost::proto::tagns_::tag::equal_to, struct boost::proto::argsns_::list2 < struct boost::phoenix::actor < struct boost::proto::exprns_::basic_expr < struct boost::proto::tagns_::tag::modulus, struct boost::proto::argsns_::list2 < struct boost::phoenix::actor < struct boost::proto::exprns_::basic_expr < struct boost::proto::tagns_::tag::terminal, struct boost::proto::argsns_::term < struct boost::phoenix::argument<1> >, 0 > >, struct boost::phoenix::actor < struct boost::proto::exprns_::basic_expr < struct boost::proto::tagns_::tag::terminal, struct boost::proto::argsns_::term<int>, 0 > > >, 2 > >, struct boost::phoenix::actor < struct boost::proto::exprns_::basic_expr < struct boost::proto::tagns_::tag::terminal, struct boost::proto::argsns_::term<int>, 0 > > >, 2
Best, Matt

On 02/15/2012 06:33 PM, Matt wrote:
I can find out the type name using non-portable typeid(Variable).name(), but the output is somewhat inconvenient (quoted below).
Is there a nice-to-type solution? It'd be esp. useful for non-C++11 compiler, where I cannot use trailing-return-type for "newActor" function nor type deduction for "f" and "g" variables -- what's the suggested type in this situation?
There is a Typeof library in Boost that provides type deduction capabilities with certain compilers (MSVC and GCC in particular). There is definitely better than writing that type explicitly, but I don't know what the best is. Maybe the author can give more info on this.
participants (2)
-
Mathias Gaunard
-
Matt