 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Anders Dalvander Guest
|
Posted: Tue Aug 19, 2003 11:41 pm Post subject: Why no logical xor operator? |
|
|
Why are there no logical-xor operator (^^) in the language? Though it
would not be able to use short-circuit evaluation, and the result
would be similar to the bitwise-xor operator (^). But if you put it
like that, there is really no need for logical-or nor logical-and
either as you could use bitwise-or and bitwise-and instead (except for
the short-circuit evaluation part).
Using:
operand-implicitly-converted-to-true = oict-true
operand-implicitly-converted-to-false = oict-false
oict-true ^^ oict-true == false
oict-false ^^ oict-true == true
oict-true ^^ oict-false == true
oict-false ^^ oict-false == false
1 ^^ 2 would thus be equal to false, as 1 is implicitly converted to
true and 2 is implicitly converted to true.
1 ^ 2 would on the other hand (still) be equal to 3, and 3 could be
implicitly converted to true.
Thus, the logical-xor operator (^^) would not behave the same as the
bitwise-xor operator (^).
// Anders
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Stefan Heinzmann Guest
|
Posted: Wed Aug 20, 2003 2:19 pm Post subject: Re: Why no logical xor operator? |
|
|
[email]google (AT) dalvander (DOT) com[/email] (Anders Dalvander) wrote in message news:<d04c84a9.0308191319.32319a05 (AT) posting (DOT) google.com>...
| Quote: | Why are there no logical-xor operator (^^) in the language?
|
Whenever I need one, I use != on boolean operands. That means, of
course, that I might have to explicitly convert the operands to
boolean, but I don't think that this is so inconvenient as to warrant
the introduction of a new operator.
In a nutshell, your proposed operator^^, if it existed, would
informally be implemented as follows:
template<typename L, typename R>
bool operator^^(const L& lhs, const R& rhs)
{
return bool(lhs) != bool(rhs);
}
Cheers
Stefan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Daniel Frey Guest
|
Posted: Thu Aug 21, 2003 5:17 am Post subject: Re: Why no logical xor operator? |
|
|
Jack Applin wrote:
| Quote: | Proposed code left-to-right? sequence point? bool args? bool result?
-------------- -------------- --------------- ---------- ------------
a ^ b no no no no
a != b no no no yes
(!a)!=(!b) no no yes yes
my_xor_func(a,b) no no yes yes
a ? !b : b yes yes yes no
a ? !b : !!b yes yes yes yes
|
Consider:
#include <iostream>
using namespace std;
struct B
{
operator bool() const { return true; }
B operator!() const { return B(); }
};
int main()
{
B a, b;
cout << ( a ? !b : !!b ) << endl; // Oops...
cout << ( a ? !static_cast
}
This also shows how the correct version should look like :)
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: [email]daniel.frey (AT) aixigo (DOT) de[/email], web: http://www.aixigo.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Thu Aug 21, 2003 5:24 pm Post subject: Re: Why no logical xor operator? |
|
|
An logxor (logical exclusive or) "operator" with the same precedence as
or can be implemented as a macro:
struct xor_dummy {};
struct xor_bool
{
template<typename T>
xor_bool(const T & value) : value(value) {}
const bool value;
};
template<typename T>
xor_bool operator or(T left, xor_dummy)
{
return xor_bool(left);
}
template<typename T>
bool operator or(xor_bool left, T right)
{
return left.value xor bool(right);
}
#define logxor or ::xor_dummy() or
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Daniel Frey Guest
|
Posted: Fri Aug 22, 2003 1:29 pm Post subject: Re: Why no logical xor operator? |
|
|
OK, the table needs some corrections, as I showed that the guarantees
given for bool args and bool results cannot be given by the proposed
code. Also, there is another important property for such an idiom: If
the arguments are integral constant expressions (ICEs), is the result
also guaranteed to be an integral constant expression? Obviously, this
is not given for a my_xor_func()-implementation. Another row should be
added to reflect whether the 'b'-part is repeated in the user's code or
not, as 'b' might be a long expression in practice, repeating it can be
a maintenance nightmare. Note that I ask "Singular 'b'" to make "yes"
the desirable answer. Here we go:
Proposed code left-to-right? sequence point? bool args? bool
result? ICE result? Singular 'b'?
-------------- -------------- --------------- ----------
------------ ----------- -------------
a ^ b no no no no
yes yes
a != b no no no no
yes yes
(!a)!=(!b) no no no no
yes yes
my_xor_func(a,b) no no yes yes
no yes
a ? !b : b yes yes no no
yes no
a ? !b : !!b yes yes no no
yes no
[* see below] yes yes yes yes
yes no
(( a bool_xor b )) yes yes yes yes
yes yes
[* = a ? !static_cast<bool>(b) : static_cast<bool>(b)]
But what is this funny "(( a bool_xor b ))"? Well, you can create some
macros that allow you such a strange syntax. Note that the
double-brackets are part of the syntax and cannot be removed! The set of
three macros (plus two internal helper macros) also provides bool_and
and bool_or. That given, what is it good for? We have && and || already,
why do we need such a stupid syntax? Well, && and || can't guarantee
that the arguments are converted to bool and that you get a bool result.
Think "operator overloads". Here's how the macros look like:
#define BOOL_DETAIL_AND_HELPER(x)
static_cast<bool>(x):false
#define BOOL_DETAIL_XOR_HELPER(x)
!static_cast<bool>(x):static_cast<bool>(x)
#define bool_and )?BOOL_DETAIL_AND_HELPER(
#define bool_or )?true:static_cast<bool>(
#define bool_xor )?BOOL_DETAIL_XOR_HELPER(
Thanks to Paul Mensonides, Fernando Cacciola and John Torjo for feedback
on the code on the boost list :)
And please note that I never claimed someone could possibly need such
stuff )
Regards, Daniel
--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: [email]daniel.frey (AT) aixigo (DOT) de[/email], web: http://www.aixigo.de
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|