Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-08-20 16:55:29


Inspired by a thread over at comp.std.c++, I suggest the following MACRO
for inclusion into Boost. Please discuss :)

//
// A logical xor operator for C++.
//
// Create a macro for the following syntax: ((a xor b)). Note that
// the bracket are part of the syntax and shall not be removed!
// The macro guarantees left-to-right evaluation, a sequence point
// between the evaluation of 'a' and 'b', conversion of the
// parameters to bool and a boolean result.
#define BOOST_DETAIL_XOR(x) !static_cast<bool>(x):static_cast<bool>(x)
#define xor )?BOOST_DETAIL_XOR(

// Usage:

#include <iostream>
using namespace std;

struct B
{
    operator bool() const { return true; }
    B operator!() const { return B(); }
};

int main()
{
    B a, b;

    // Oops...
    cout << ( a != b ) << endl;
    cout << ( a ? !b : !!b ) << endl;

    // Works, but long, ugly syntax and repeating 'b':
    cout << ( a ? !static_cast<bool>(b) : static_cast<bool>(b) ) << endl;

    // Cool?
    cout << (( a xor b )) << endl;
}

Regards, Daniel

PS: No, don't take this too serious :o)

-- 
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: daniel.frey_at_[hidden], web: http://www.aixigo.de

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk