Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #542

Re: Why no logical xor operator?

Message-ID <85e151f2-c349-49b4-8b38-1217e26e70e0@googlegroups.com> (permalink)
Newsgroups comp.std.c++
From harris.pc@googlemail.com
Subject Re: Why no logical xor operator?
Organization unknown
References <d04c84a9.0308191319.32319a05@posting.google.com> <s_A0b.203635$YN5.141578@sccrnsc01> <bi0aas$rfh$1@swifty.westend.com> <bi4odc$8ac$1@swifty.westend.com>
Date 2012-09-06 13:05 -0600

Show all headers | View raw


On Friday, August 22, 2003 9:29:22 PM UTC+8, Daniel Frey wrote:
> 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 :o)
>
> Regards, Daniel
>
> --
> Daniel Frey
>
> aixigo AG - financial training, research and technology
> Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
> fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
> eMail: daniel.frey@aixigo.de, 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                       ]



I had to make some changes for it to work on MSVC 2010 -- the brackets
weren't expanding correctly.
So it now looks like this:

#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_DETAIL_OPEN (
#define BOOL_DETAIL_CLOSE )

#define bool_and BOOL_DETAIL_CLOSE ? BOOL_DETAIL_AND_HELPER BOOL_DETAIL_OPE=
N
#define bool_or BOOL_DETAIL_CLOSE ? true:static_cast<bool> BOOL_DETAIL_OPEN
#define bool_xor BOOL_DETAIL_CLOSE ? BOOL_DETAIL_XOR_HELPER BOOL_DETAIL_OPE=
N

And works great :)



--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | Next | Find similar


Thread

Re: Why no logical xor operator? harris.pc@googlemail.com - 2012-09-06 13:05 -0600

csiph-web