Boolean Logic

As can be seen in Basic/Bool Logic, logic operation's with an ntuple<> check wether every element converted to bool returns false, this operation "short circuits" [1] as soon as an element return's true.

The testing of elements also stops if an element is found that isn't testable (convertable to bool).

#include <iostream>
#include <ostream>
#include <iomanip>

#include "imp/ntuple.hpp"

struct NonConvertable {};

int main()
{
  imp::ntuple< int, NonConvertable >
    True( 1, NonConvertable() ),
    False( 0, NonConvertable() )
  ;
  
  if ( ! bool( True ) ||  bool( False ) )
  {
    std::cerr << std::boolalpha << "True:  " << bool( True ) << std::endl;
    std::cerr << std::boolalpha << "False: " << bool( False ) << std::endl;
  }
}

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed


However the first element must be testable.

#include <cassert>
#include "imp/ntuple.hpp"

struct NonConvertable {};

int main()
{
  imp::ntuple< NonConvertable, int > NoGood = imp::ntuple_( NonConvertable(), 0 );
  
  /* This Shouldn't compile
  */
  assert( ! bool( NoGood ) );
}

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Warning

../ntuple-bool.hpp:55:
: '?' : cannot convert from 'const imp::ntuple_s::cons<Head,Tail>::first_type' to 'bool'
        with
        [
            Head=NonConvertable,
            Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type>
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
../ntuple-bool.hpp:53:
while compiling class-template member function 'bool imp::ntuple_s::cons_bool<Head,Tail>::any_true(const imp::ntuple_s::cons<Head,Tail> &)'
        with
        [
            Head=NonConvertable,
            Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type>
        ]
../ntuple-cons.hpp:221:
see reference to class template instantiation 'imp::ntuple_s::cons_bool<Head,Tail>' being compiled
        with
        [
            Head=NonConvertable,
            Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type>
        ]
../ntuple-cons.hpp:218:
while compiling class-template member function 'imp::ntuple_s::cons<Head,Tail>::operator`imp::ntuple_s::op_bool_result_t'(void) const'
        with
        [
            Head=NonConvertable,
            Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type>
        ]
../ntuple.hpp:88:
see reference to class template instantiation 'imp::ntuple_s::cons<Head,Tail>' being compiled
        with
        [
            Head=NonConvertable,
            Tail=imp::ntuple_s::cons<int,imp::ntuple_s::null_type>
        ]
./gensrc/more-bool-id-nonconvert-fail.cpp:8:
see reference to class template instantiation 'imp::ntuple_s::ntuple<A0,A1>' being compiled
        with
        [
            A0=NonConvertable,
            A1=int
        ]

[1]

"Short circuiting" is the behaviour of the inbuilt logic operators && and ||

  • && doesn't evaluate its second argument if it's first is false
  • || doesn't evaluate its second argument if it's first is true