class template

std::logical_and

<functional>
template <class T> struct logical_and;
Logical AND function object class
This class defines function objects for the "and" logical operation (&&).

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

logical_and has its operator() member defined such that it returns true if both its arguments are true using operator&&, and false otherwise.

This class is derived from binary_function and is defined as:

1
2
3
4
template <class T> struct logical_and : binary_function <T,T,bool> {
  bool operator() (const T& x, const T& y) const
    {return x&&y;}
};


Objects of this class can be used with several standard algorithms (see algorithm).

Members

bool operator() (const T& x, const T& y)
Member function returning the result of x&&y.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// logical_and example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  bool foo[] = {true,false,true,false};
  bool bar[] = {true,true,false,false};
  bool result[4];
  transform (foo, foo+4, bar, result, logical_and<bool>() );
  cout << boolalpha << "Logical AND:\n";
  for (int i=0; i<4; i++)
	  cout << foo[i] << " AND " << bar[i] << " = " << result[i] << "\n";
  return 0;
}


Output:

Logical AND:
true AND true = true
false AND true = false
true AND false = false
false AND false = false

See also