Boolean Logic

Contents

Conversion to bool and operator ! ()

ntuple<> supports logic operations via a member operator ! () and via a conversion to a member pointer type operator member_pointer_type (), this prevents unwanted conversion's to integral types (int etc).

void const * null = 0;
ntuple< int, void const * > a( 1, null ), b( 0, null ), c( 0, null ), d( 0, "" );

bool ba = a, bb = b, bc = c, bd = d;

std::cout << std::boolalpha;

std::cout << a << " => " << ba << std::endl;
std::cout << b << " => " << bb << std::endl;
std::cout << c << " => " << bc << std::endl;
std::cout << d << " => " << bd << std::endl;

CHECK( ba && !bb && !bc && bd );


/* operator !()
*/
ba = !a, bb = !b, bc = !c, bd = !d;
  
std::cout << "!" << a << " => " << ba << std::endl;
std::cout << "!" << b << " => " << bb << std::endl;
std::cout << "!" << c << " => " << bc << std::endl;
std::cout << "!" << d << " => " << bd << std::endl;

CHECK( !ba && bb && bc && !bd );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(1,00000000) => true
(0,00000000) => false
(0,00000000) => false
(0,004101D5) => true
!(1,00000000) => false
!(0,00000000) => true
!(0,00000000) => true
!(0,004101D5) => false

Conversion's via ntuple<>'s conversion operator

As shown above an ntuple<> can be converted to a bool though the actual conversion is tuple -> undocumented_member_pointer_type -> bool.

Other conversion's such as tuple -> int and tuple -> void * don't work and generate an error message:

ntuple< int, int > tuple( 1, 2 );

int i = tuple;

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Warning

./gensrc/basic-bool-id-int-fail.cpp:25:
: 'initializing' : cannot convert from 'imp::ntuple_s::ntuple<A0,A1>' to 'const void *'
        with
        [
            A0=int,
            A1=int
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

ntuple< int, int > tuple( 1, 2 );

void const *p = tuple;

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Warning

./gensrc/basic-bool-id-int-fail.cpp:25:
: 'initializing' : cannot convert from 'imp::ntuple_s::ntuple<A0,A1>' to 'const void *'
        with
        [
            A0=int,
            A1=int
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

More on bool ...