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
#include
#include
#include
#include <functional>
#include <list>
using namespace boost::lambda;
template<typename RET>
RET InvokeReturn(boost::function f, const RET& retVal)
{
printf("in InvokeReturn!\n");
f();
return retVal;
}
bool InvokeReturnTrue(boost::function f)
{
return InvokeReturn(f, true);
}
bool InvokeReturnFalse(boost::function f)
{
return InvokeReturn(f, false);
}
boost::function
MakeReturnTrue(boost::function f)
{
printf("in MakeReturnTrue!\n");
return boost::lambda::bind(&InvokeReturnTrue, f);
}
boost::function
MakeReturnFalse(boost::function f)
{
printf("in MakeReturnFalse!\n");
return boost::lambda::bind(&InvokeReturnFalse, f);
}
template
bool
IfNotEndThenElseReturn(const ITERATORTYPE& it,
CONTAINERTYPE& container, boost::function thenBranch,
boost::function elseBranch)
{
printf("in IfNotEndThenElseReturn!\n");
std::not_equal_to<ITERATORTYPE> ne;
return boost::lambda::if_then_else_return(
boost::lambda::bind(&std::not_equal_to<ITERATORTYPE>::operator(),
ne, it, container.end()),
MakeReturnTrue(thenBranch),
MakeReturnFalse(elseBranch))();
}
void thenBranch()
{
printf("in then branch!\n");
}
void elseBranch()
{
printf("in else branch!\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("beginning execution!\n");
std::list<int> l;
l.push_back(8);
l.push_back(7);
l.push_back(6);
l.push_back(5);
std::list<int>::iterator it = l.end();
std::list<int>::iterator it2 = l.begin();
bool b = IfNotEndThenElseReturn(it, l, bind(&thenBranch),
bind(&elseBranch));
printf("b is %s\n", b ? "true" : "false");
bool b = IfNotEndThenElseReturn(it2, l, bind(&thenBranch),
bind(&elseBranch));
printf("b is %s\n", b ? "true" : "false");
printf("finished!\n");
return 0;
}