Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++/CLI
Article

Logical XOR operator

Rate me:
Please Sign up or sign in to vote.
3.10/5 (13 votes)
5 Sep 2002 185.9K   18   31
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


Written By
Croatia Croatia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: What is this all about? Pin
Goran Mitrovic7-Sep-02 6:39
Goran Mitrovic7-Sep-02 6:39 
GeneralRe: What is this all about? Pin
Tim Smith9-Sep-02 8:53
Tim Smith9-Sep-02 8:53 
The problem is that there are limits. I don't know about this specific example, however, the more deeply nested your templates get, the more chance the optimizer is going to produce bad code. I have seen many cases with STL where doing loops the right way ended up with poor execution speed because of register exhaustion during the optimization phase. It isn't that the compiler really ran out of registers, it is just that it doesn't fully utilize the registers it has. This results in such problems as using stack space to store temporary values.

Like I said, I don't know about this specific case, but in my own work, I have backed off of using templates when other simpler solutions exist. They just don't work as well as we have been lead to believe.

Tim Smith

"Programmers are always surrounded by complexity; we can not avoid it... If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather that part of the solution."
Hoare - 1980 ACM Turing Award Lecture
GeneralRe: What is this all about? Pin
Goran Mitrovic9-Sep-02 10:42
Goran Mitrovic9-Sep-02 10:42 
GeneralCool Beans Pin
Anonymous6-Sep-02 9:46
Anonymous6-Sep-02 9:46 
GeneralRe: Cool Beans Pin
Goran Mitrovic6-Sep-02 14:35
Goran Mitrovic6-Sep-02 14:35 
GeneralNice :-) Pin
Nish Nishant6-Sep-02 6:33
sitebuilderNish Nishant6-Sep-02 6:33 

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.