Click here to Skip to main content
15,394,193 members
Articles / Programming Languages / C++/CLI
Article
Posted 5 Sep 2002

Stats

182.2K views
18 bookmarked

Logical XOR operator

Rate me:
Please Sign up or sign in to vote.
3.10/5 (13 votes)
5 Sep 2002
A simple implementation of a logical xor operator.

Introduction

Occasionally I come to a need of a logical xor operator (^^) that, obviously, doesn't exist in C++. However, it can be rather easily implemented.

Code listing

#define log_xor || log_xor_helper() ||


struct log_xor_helper {
    bool value;
};

template<typename LEFT>
log_xor_helper &operator ||(const LEFT &left, log_xor_helper &xor) {
    xor.value = (bool)left;
    return xor;
}

template<typename RIGHT>
bool operator ||(const log_xor_helper &xor, const RIGHT &right) {
    return xor.value ^ (bool)right;
}

Examples

0 log_xor 0 == false
0 log_xor 5 == true
8 log_xor 0 == true
8 log_xor 5 == false

Other details

  • Although they don't have to have the same type, operands should be castable into a simple boolean.
  • Precedence of log_xor operator is the same as logical or's (||) one.
  • If you decide to use it, I suggest you to add log_xor keyword to the usertype.dat file and insert somewhere the #pragma warning(disable: 4800) line.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Goran Mitrovic
Croatia Croatia
No Biography provided

Comments and Discussions

 
QuestionSome additions.... Pin
Alexandre Bencz28-Oct-16 8:17
MemberAlexandre Bencz28-Oct-16 8:17 
GeneralThere is an easy way Pin
miguelps4-Dec-08 10:24
Membermiguelps4-Dec-08 10:24 
GeneralRe: There is an easy way Pin
Goran Mitrovic4-Dec-08 10:43
MemberGoran Mitrovic4-Dec-08 10:43 
GeneralNice code but... Pin
MINUSVOTER10-Apr-06 1:34
MemberMINUSVOTER10-Apr-06 1:34 
GeneralRe: Nice code but... Pin
Goran Mitrovic10-Apr-06 1:58
MemberGoran Mitrovic10-Apr-06 1:58 
General#pragma warning(disable: 4800) Pin
Alistair Milne1-Mar-06 11:06
MemberAlistair Milne1-Mar-06 11:06 
GeneralNeat but more trouble than !a != !b Pin
vladsch27-Feb-04 4:48
Membervladsch27-Feb-04 4:48 
GeneralRe: Neat but more trouble than !a != !b Pin
Alistair Milne1-Mar-06 11:15
MemberAlistair Milne1-Mar-06 11:15 
GeneralRe: Neat but more trouble than !a != !b Pin
vladsch1-Mar-06 12:41
Membervladsch1-Mar-06 12:41 
GeneralRe: Neat but more trouble than !a != !b Pin
Alistair Milne1-Mar-06 23:22
MemberAlistair Milne1-Mar-06 23:22 
GeneralBut there is one ... Pin
Anonymous8-Dec-02 19:34
MemberAnonymous8-Dec-02 19:34 
I thought "!=" was the logical xor operator (think about it) ...
David F

GeneralRe: But there is one ... Pin
Goran Mitrovic18-Dec-02 11:36
MemberGoran Mitrovic18-Dec-02 11:36 
GeneralRe: But there is one ... Pin
Anonymous15-Jun-03 10:37
MemberAnonymous15-Jun-03 10:37 
GeneralRe: But there is one ... Pin
Goran Mitrovic15-Jun-03 11:27
MemberGoran Mitrovic15-Jun-03 11:27 
GeneralRe: But there is one ... Pin
Anonymous25-Feb-04 15:35
MemberAnonymous25-Feb-04 15:35 
GeneralRe: But there is one ... Pin
shiggity s30-Mar-04 21:09
Membershiggity s30-Mar-04 21:09 
GeneralRe: But there is one ... Pin
Anonymous17-Apr-04 6:17
MemberAnonymous17-Apr-04 6:17 
GeneralRe: But there is one ... Pin
Anonymous17-Apr-04 6:21
MemberAnonymous17-Apr-04 6:21 
GeneralRe: But there is one ... Pin
Anonymous17-Apr-04 6:29
MemberAnonymous17-Apr-04 6:29 
GeneralRe: But there is one ... Pin
fondue4fromages9-Aug-05 5:20
Memberfondue4fromages9-Aug-05 5:20 
GeneralToo bad... Pin
UltraJoe9-Sep-02 8:42
MemberUltraJoe9-Sep-02 8:42 
QuestionWhat is this all about? Pin
Anonymous6-Sep-02 16:43
MemberAnonymous6-Sep-02 16:43 
AnswerRe: What is this all about? Pin
Goran Mitrovic7-Sep-02 3:01
MemberGoran Mitrovic7-Sep-02 3:01 
GeneralRe: What is this all about? Pin
Roman Nurik7-Sep-02 6:04
MemberRoman Nurik7-Sep-02 6:04 
GeneralRe: What is this all about? Pin
Goran Mitrovic7-Sep-02 6:24
MemberGoran Mitrovic7-Sep-02 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.