logical_or Struct

A predefined function object that performs the logical disjunction operation (operator||) on its arguments.

template<class Type = void>
   struct logical_or : public binary_function<Type, Type, bool> 
   {
      bool operator()(
         const Type& Left, 
         const Type& Right
      ) const;
   };

// specialized transparent functor for operator||
template<>
   struct logical_or<void>
   {
      template<class Type1, class Type2>
      auto operator()(Type1&& Left, Type2&& Right) const
         -> decltype(std::forward<Type1>(Left)
            || std::forward<Type2>(Right));
   };

Parameters

  • Type, Type1, Type2
    Any type that supports an operator|| that takes operands of the specified or inferred types.

  • Left
    The left operand of the logical disjunction operation. The unspecialized template takes an lvalue reference argument of type Type. The specialized template does perfect forwarding of lvalue and rvalue reference arguments of inferred type Type1.

  • Right
    The right operand of the logical disjunction operation. The unspecialized template takes an lvalue reference argument of type Type. The specialized template does perfect forwarding of lvalue and rvalue reference arguments of inferred type Type2.

Return Value

The result of Left||Right. The specialized template does perfect forwarding of the result, which has the type that's returned by operator||.

Remarks

For user-defined types, there is no short-circuiting of operand evaluation. Both arguments are evaluated by operator||.

Example

// functional_logical_or.cpp
// compile with: /EHsc
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>

int main( )
{
   using namespace std;
   deque <bool> d1, d2, d3( 7 );
   deque <bool>::iterator iter1, iter2, iter3;

   int i;
   for ( i = 0 ; i < 7 ; i++ )
   {
      d1.push_back((bool)((rand() % 2) != 0));
   }

   int j;
   for ( j = 0 ; j < 7 ; j++ )
   {
      d2.push_back((bool)((rand() % 2) != 0));
   }

   cout << boolalpha;    // boolalpha I/O flag on

   cout << "Original deque:\n d1 = ( " ;
   for ( iter1 = d1.begin( ) ; iter1 != d1.end( ) ; iter1++ )
      cout << *iter1 << " ";
   cout << ")" << endl;

   cout << "Original deque:\n d2 = ( " ;
   for ( iter2 = d2.begin( ) ; iter2 != d2.end( ) ; iter2++ )
      cout << *iter2 << " ";
   cout << ")" << endl;

   // To find element-wise disjunction of the truth values
   // of d1 & d2, use the logical_or function object
   transform( d1.begin( ), d1.end( ), d2.begin( ),
      d3.begin( ), logical_or<bool>( ) );
   cout << "The deque which is the disjuction of d1 & d2 is:\n d3 = ( " ;
   for ( iter3 = d3.begin( ) ; iter3 != d3.end( ) ; iter3++ )
      cout << *iter3 << " ";
   cout << ")" << endl;
}
Original deque:
 d1 = ( true true false false true false false )
Original deque:
 d2 = ( false false false true true true true )
The deque which is the disjuction of d1 & d2 is:
 d3 = ( true true false true true true true )

Requirements

Header: <functional>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

Standard Template Library