[Phoenix] Simple if_ issue

I'm trying to do a simple if/else statement using boost::phoenix and I seem to mis-understanding something. namespace bp = boost::phoenix; namespace bpa = bp::arg_names; boost::function< std::string () > test1 = bp::val("true"); boost::function< std::string (bool) > test2 = bp::if_(bpa::arg1) [ bp::val("true") ] .else_ [ bp::val("false") ]; The first one works, but the second one doesn't. I get an error about not being able to convert void to std::string: cannot convert parameter 1 from 'void' to 'const std::basic_string<_Elem,_Traits,_Ax> & What do I need to do in the second test to get it to compile? Can I tell if_ that its return type is not void? --- Aaron Wright

On Mon, Jul 2, 2012 at 12:51 PM, <Aaron_Wright@selinc.com> wrote: <snip code>
What do I need to do in the second test to get it to compile? Can I tell if_ that its return type is not void?
Sadly no -- this was a point of confusion for me too. The correct way to do this is with if_else <http://www.boost.org/doc/libs/1_50_0/libs/spirit/phoenix/doc/html/phoenix/composite.html#phoenix.composite.statement.if__else___statement>. HTH, Nate

Aaron, On Mon, Jul 2, 2012 at 1:05 PM, Nathan Crookston <nathan.crookston@gmail.com> wrote:
On Mon, Jul 2, 2012 at 12:51 PM, <Aaron_Wright@selinc.com> wrote: <snip code>
What do I need to do in the second test to get it to compile? Can I tell if_ that its return type is not void?
Sadly no -- this was a point of confusion for me too. The correct way to do this is with if_else
Sorry to reply twice. The advice is correct, but the link is wrong (I'm not sure where if_else is documented. . .). The following shows usage: #include <boost/function.hpp> #include <boost/spirit/home/phoenix.hpp> #include <iostream> namespace px = boost::phoenix; using px::arg_names::arg1; int main() { boost::function<std::string(bool)> f = px::if_else(arg1, "true", "false"); std::cout << f(true) << std::endl; std::cout << f(false) << std::endl; return 0; } HTH, Nate

On 7/2/2012 12:10 PM, Nathan Crookston wrote:
Aaron,
On Mon, Jul 2, 2012 at 1:05 PM, Nathan Crookston <nathan.crookston@gmail.com> wrote:
On Mon, Jul 2, 2012 at 12:51 PM, <Aaron_Wright@selinc.com> wrote: <snip code>
What do I need to do in the second test to get it to compile? Can I tell if_ that its return type is not void?
Sadly no -- this was a point of confusion for me too. The correct way to do this is with if_else
Sorry to reply twice. The advice is correct, but the link is wrong (I'm not sure where if_else is documented. . .). <snip>
http://boost-sandbox.sourceforge.net/libs/phoenix/doc/html/phoenix/modules/o... And let me fill in the rationale. The if_else function models the ternary conditional operator (cond ? <true-case> : <false-case>). This is an expression, which has a type and a value. When you use if_(x)[y].else_[z], you're emulating if(x){y;}else{z;}, which is a statement. Statements don't have types or values in C++. Why does it matter? Because y and z might have incompatible types, and that's OK in an if/else statement, but not in a conditional expression. HTH, -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (3)
-
Aaron_Wright@selinc.com
-
Eric Niebler
-
Nathan Crookston