Click here to Skip to main content
Licence 
First Posted 5 Sep 2002
Views 154,320
Bookmarked 18 times

Logical XOR operator

By | 5 Sep 2002 | Article
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

About the Author

Goran Mitrovic



Croatia Croatia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThere is an easy way Pinmembermiguelps10:24 4 Dec '08  
GeneralRe: There is an easy way PinmemberGoran Mitrovic10:43 4 Dec '08  
GeneralNice code but... PinmemberMINUSVOTER1:34 10 Apr '06  
GeneralRe: Nice code but... PinmemberGoran Mitrovic1:58 10 Apr '06  
General#pragma warning(disable: 4800) PinmemberAlistair Milne11:06 1 Mar '06  
GeneralNeat but more trouble than !a != !b Pinmembervladsch4:48 27 Feb '04  
GeneralRe: Neat but more trouble than !a != !b PinmemberAlistair Milne11:15 1 Mar '06  
GeneralRe: Neat but more trouble than !a != !b Pinmembervladsch12:41 1 Mar '06  
GeneralRe: Neat but more trouble than !a != !b PinmemberAlistair Milne23:22 1 Mar '06  
GeneralBut there is one ... PinsussAnonymous19:34 8 Dec '02  
GeneralRe: But there is one ... PinmemberGoran Mitrovic11:36 18 Dec '02  
GeneralRe: But there is one ... PinsussAnonymous10:37 15 Jun '03  
GeneralRe: But there is one ... PinmemberGoran Mitrovic11:27 15 Jun '03  
GeneralRe: But there is one ... PinsussAnonymous15:35 25 Feb '04  
GeneralRe: But there is one ... Pinmembershiggity s21:09 30 Mar '04  
GeneralRe: But there is one ... PinsussAnonymous6:17 17 Apr '04  
GeneralRe: But there is one ... PinsussAnonymous6:21 17 Apr '04  
GeneralRe: But there is one ... PinsussAnonymous6:29 17 Apr '04  
GeneralRe: But there is one ... Pinmemberfondue4fromages5:20 9 Aug '05  
GeneralToo bad... PinmemberUltraJoe8:42 9 Sep '02  
QuestionWhat is this all about? PinsussAnonymous16:43 6 Sep '02  
AnswerRe: What is this all about? PinmemberGoran Mitrovic3:01 7 Sep '02  
GeneralRe: What is this all about? PinmemberRoman Nurik6:04 7 Sep '02  
GeneralRe: What is this all about? PinmemberGoran Mitrovic6:24 7 Sep '02  
GeneralRe: What is this all about? PinmemberGoran Mitrovic6:39 7 Sep '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 6 Sep 2002
Article Copyright 2002 by Goran Mitrovic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid