if_then_else_return (boost::lambda) question
Greetings!
I'm fairly new to boost and have a little problem with if_then_else_return.
Can anyone tell me the problem with the following short program? [It seems
that my 'thenBranch' and 'elseBranch' functions are never called and
if_then_else_return' always returns true (as if the 'then' branch were
called)].
Many thanks in advance for any help you may offer!
P.S. (A more general C++ q: A little improvement I'd like to make is to
drop the ITERATORTYPE type argument from IfNotEndThenElseReturn and use
CONTAINERTYPE::iterator instead, but the compiler won't allow that. Anyone
know why?). Thanks again!
============================================
#include
AMDG Jonathan Leonard wrote:
I'm fairly new to boost and have a little problem with if_then_else_return. Can anyone tell me the problem with the following short program? [It seems that my 'thenBranch' and 'elseBranch' functions are never called and if_then_else_return' always returns true (as if the 'then' branch were called)].
The problem is that Boost.Lambda doesn't automatically call the boost::function. Thus, evaluating the lambda expression returns a boost::function, boost::function implicitly converts to bool. (false iff the boost::function is empty.) If you use boost::lambda::bind(MakeReturnTrue(thenBranch)), it should work.
P.S. (A more general C++ q: A little improvement I'd like to make is to drop the ITERATORTYPE type argument from IfNotEndThenElseReturn and use CONTAINERTYPE::iterator instead, but the compiler won't allow that. Anyone know why?). Thanks again!
typename CONTAINERTYPE::iterator works for me. Did you forget typename? In Christ, Steven Watanabe
Steven Watanabe wrote:
Jonathan Leonard wrote:
I'm fairly new to boost and have a little problem with if_then_else_return. Can anyone tell me the problem with the following short program? [It seems that my 'thenBranch' and 'elseBranch' functions are never called and if_then_else_return' always returns true (as if the 'then' branch were called)].
The problem is that Boost.Lambda doesn't automatically call the boost::function. Thus, evaluating the lambda expression returns a boost::function, boost::function implicitly converts to bool. (false iff the boost::function is empty.) If you use boost::lambda::bind(MakeReturnTrue(thenBranch)), it should work.
Awesome! That indeed does work.
P.S. (A more general C++ q: A little improvement I'd like to make is to drop the ITERATORTYPE type argument from IfNotEndThenElseReturn and use CONTAINERTYPE::iterator instead, but the compiler won't allow that. Anyone know why?). Thanks again!
typename CONTAINERTYPE::iterator works for me. Did you forget typename?
Yep. Thank you!
participants (2)
-
Jonathan Leonard
-
Steven Watanabe