Hi,
I want to emulate the 'implies' (=>) logical operator in C++. There's no built-in support for it.
For this purpose, I firstly defined a macro :
# define implies(a, b) !(a) || (b)
And it works fine, especially if a is false then b is not evaluated. But syntactically speaking, this is pretty heavy, for example:
assert (implies (list.is_empty (),/* => */!result));
So I tried another approach, allowing to write in a more natural way, i.e. something like : assert (list.is_empty () implies !result);
To this end, I defined a class that handles the implies computation :
You'll get :
haaa
0 => kikette = 1
which means that "kikette" was called, and it should not be!
There is certainly a way to get this to work, I have try to move the expressions so that there are only 'and', but it doesn't work, second part of implies expressions get systematically evaluated.
Does the someone have a solution to this problem?
--
Globos
I want to emulate the 'implies' (=>) logical operator in C++. There's no built-in support for it.
For this purpose, I firstly defined a macro :
# define implies(a, b) !(a) || (b)
And it works fine, especially if a is false then b is not evaluated. But syntactically speaking, this is pretty heavy, for example:
assert (implies (list.is_empty (),/* => */!result));
So I tried another approach, allowing to write in a more natural way, i.e. something like : assert (list.is_empty () implies !result);
To this end, I defined a class that handles the implies computation :
Code:
# define implies || ImpliesHelper () ||
//! Helper objects, used in the computation of implies expressions.
class ImpliesHelper
{
friend ImpliesHelper& operator|| (bool left, ImpliesHelper& ih);
public:
bool operator|| (bool right)
{
return !_value || right;
}
private:
bool _value;
};
ImpliesHelper& operator|| (bool left, ImpliesHelper& ih)
{
ih._value = left;
return ih;
}
[code]
The syntax effect is reached, but there is a big problem with this : everything is evaluated in a expression, for example, if you write :
[code]
Boolean kikette ()
{
cout << "haaa" << endl;
return true;
}
int main ()
{
cout << "0 => kikette = " << (false implies kikette ()) << endl;
return 0;
}
haaa
0 => kikette = 1
which means that "kikette" was called, and it should not be!
There is certainly a way to get this to work, I have try to move the expressions so that there are only 'and', but it doesn't work, second part of implies expressions get systematically evaluated.
Does the someone have a solution to this problem?
--
Globos