M
marco_segurini
Hi,
when the following code is executed
bool Test1();
bool Test2();
....
if (Test1() || Test2()) { ... }
....
Test2 is executed only if Test1 returns false.
I am trying to create a predicate for find_if with the same behaviour:
find_if(iterBegin,iterEnd, Test1 || Test2)
For do this I have modificated the boost::compose_f_gx_hx template in
this way:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
/* class for the compose_gx_op_hx adapter
*/
template <class OP1, class OP2, class OP3>
class compose_gx_op_hx_t
: public std::unary_function<typename OP2::argument_type,
typename OP1::result_type>
{
private:
OP1 op1; // process: op2(x) op1 op3(x)
OP2 op2;
OP3 op3;
public:
// constructor
compose_gx_op_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {
}
// function call
typename OP1::result_type
operator()(const typename OP2::argument_type& x) const {
return op2(x) || op3(x);
// return op1(op2(x),op3(x));
}
};
/* convenience functions for the compose_gx_op_hx adapter
*/
template <class OP1, class OP2, class OP3>
inline compose_gx_op_hx_t<OP1,OP2,OP3>
compose_gx_op_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
return compose_gx_op_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
class Test1
: public std::unary_function<int,bool>
{
public:
bool operator()(int) const { std::cout << "Test1" << std::endl;
return true; }
};
class Test2
: public std::unary_function<int,bool>
{
public:
bool operator()(int) const { std::cout << "Test2" << std::endl;
return false; }
};
int main()
{
std::vector<int> vInt(1,1);
std::find_if(vInt.begin(), vInt.end(),
compose_gx_op_hx(std::logical_or<bool>(), Test1(), Test2()));
return 0;
}
The problem is that not a *general* solution because the
logical_operator is hardcoded (|| in this case) even if it is the
first template parameter.
If I use the commented statement
return op1(op2(x),op3(x));
instead of
return op2(x) || op3(x);
both Test1() and Test2() are always called and there is no short
evaluation.
Many thanks for any help.
Marco.
when the following code is executed
bool Test1();
bool Test2();
....
if (Test1() || Test2()) { ... }
....
Test2 is executed only if Test1 returns false.
I am trying to create a predicate for find_if with the same behaviour:
find_if(iterBegin,iterEnd, Test1 || Test2)
For do this I have modificated the boost::compose_f_gx_hx template in
this way:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
/* class for the compose_gx_op_hx adapter
*/
template <class OP1, class OP2, class OP3>
class compose_gx_op_hx_t
: public std::unary_function<typename OP2::argument_type,
typename OP1::result_type>
{
private:
OP1 op1; // process: op2(x) op1 op3(x)
OP2 op2;
OP3 op3;
public:
// constructor
compose_gx_op_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
: op1(o1), op2(o2), op3(o3) {
}
// function call
typename OP1::result_type
operator()(const typename OP2::argument_type& x) const {
return op2(x) || op3(x);
// return op1(op2(x),op3(x));
}
};
/* convenience functions for the compose_gx_op_hx adapter
*/
template <class OP1, class OP2, class OP3>
inline compose_gx_op_hx_t<OP1,OP2,OP3>
compose_gx_op_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
return compose_gx_op_hx_t<OP1,OP2,OP3>(o1,o2,o3);
}
class Test1
: public std::unary_function<int,bool>
{
public:
bool operator()(int) const { std::cout << "Test1" << std::endl;
return true; }
};
class Test2
: public std::unary_function<int,bool>
{
public:
bool operator()(int) const { std::cout << "Test2" << std::endl;
return false; }
};
int main()
{
std::vector<int> vInt(1,1);
std::find_if(vInt.begin(), vInt.end(),
compose_gx_op_hx(std::logical_or<bool>(), Test1(), Test2()));
return 0;
}
The problem is that not a *general* solution because the
logical_operator is hardcoded (|| in this case) even if it is the
first template parameter.
If I use the commented statement
return op1(op2(x),op3(x));
instead of
return op2(x) || op3(x);
both Test1() and Test2() are always called and there is no short
evaluation.
Many thanks for any help.
Marco.