Pass condition as parameter

First of all, to explain what I'm trying to do:

void Foo(int &num, bool condition); Foo(x, x > 3); 

This code would basically evaluate the bool condition before calling the function, and then pass a clear true or false. I am looking for a way to make this pass a condition, so I could do something like this:

 void Foo(int &num, bool condition) { while(!condition) { num = std::rand(); } } 

I know that there can be a workaround by passing the line containing the condition, and we will analyze the latter, and I am working on it now, but I consider it rather inefficient. The accepted answer will be the one who explains the solution in any other way next to where the line containing the condition is parsed, or the answer that explains that this way of passing the conditions is impossible.

Thanks in advance

+4
source share
7 answers

One example using the standard functor library:

 #include <functional> template<class UnaryPred> void func(int& num, UnaryPred predicate) { while(!predicate(num)) num = std::rand(); } void test() { int i = 0; func(i, std::bind1st(std::greater<int>(), 3)); } 

See the <functional> documentation for what C ++ already provides you with ready-made files.

If your compiler has sufficient support (e.g. GCC 4.5 or VC10), you can also switch to lambda functions . For instance. using the same func() as above:

 func(i, [](int num) { return num > 3; }); 
+11
source

this is called functional programming.

here is a snippet using boost :: phoenix http://www.boost.org/doc/libs/1_43_0/libs/spirit/phoenix/doc/html/phoenix/starter_kit.html :

 using namespace boost::phoenix; Foo(x, (cref(x) < 3)); // (cref(x) < 3) is expression that creates function object template<class C> // condition is a monster, make it generic void Foo(int &num, C condition) { while(!condition()) // notice you call condition as function { rand(num); } } 
+3
source

What you need to do is create a function that checks your status, and then pass a pointer to that function as a parameter. See http://www.cprogramming.com/tutorial/function-pointers.html for detailed / tutorials on how to do this.

+2
source

A plausible way to do this would be given to the functor - and the object that behaves as a function - of the called function, and the called function will call the functor to re-evaluate the condition at each iteration of the loop.

Parsing a string will not work; the called function does not have access to the local variables of the calling function.

+2
source

Pass the function instead of Foo . Then you can β€œdelay” the execution of the expression, waiting for the call to this function.

+1
source

As others have said, pass a functor object to Foo (). STL usually uses this approach. For instance:

 template< class Func > void Foo(int &num, Func condition) { while(!condition()) { rand(num); } } struct GreaterThanThree { int& _num; GreaterThanThree(int &num) : _num(num) {} bool operator()() const { return (_num > 3); } }; Foo(x, GreaterThanThree(x)); 
+1
source

You can do this without templates or forcing (the commentator called this "C-style", which I think is right).

 /* have Foo take a pointer to a function that returns bool */ void Foo(int &num, bool (*fcn)(int)) { while(!fcn(num)) { num = std::rand(); } } /* You can have all the Comparators you want, as long as they have the same signature */ bool ComparatorOne(int x) { return x > 3 ? true : false; } bool ComparatorTwo(int x) { return x < 10 ? true : false; } /* and this is how you call it */ int n; Foo(n, ComparatorOne); Foo(n, ComparatorTwo); 

Edit

Please note that your comparator may accept various parameters if they are consistent.

+1
source

All Articles