Let's say I have to evaluate multiple types of conditions
if(A > B), if(A >= B)
and the opposites
if(A < B), if(A <= B)
Is there a way in C++ to pass the relational operators as parameters themselves, so that I might be able to have a function that looks something like the following
function name (argument B) <---this is not a bool value
{
if(B)
{
//run code here
}
}
2 replies to this topic
#1
Posted 30 August 2010 - 02:56 PM
|
|
|
#2
Posted 30 August 2010 - 05:08 PM
The Boost library has some support for passing strings that represent an expression.
#3
Posted 30 August 2010 - 09:28 PM
Sounds like you could use function objects.
#include <functional>
template < class Pred >
void foo( int a, int b, Pred pred )
{
bool result = pred( a, b );
if ( result )
{
//
// run code
} // if
}
int main( int argc, char* argv[] )
{
int a = 0, b = 1;
//
// run code if a < b
foo( a, b, std::less<int>() );
//
// run code if a > b
foo( a, b, std::greater<int>() );
return 0;
}
Functional defines some generic ones, but you can create your own if need be.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









