Share via


logical_and 構造体

引数に対して論理積演算 (operator&&) を実行する定義済みの関数オブジェクト。

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

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

パラメーター

  • Type, Type1, Type2
    指定または推論された型のオペランドを受け取る operator&& をサポートする任意の型。

  • Left
    論理積演算の左オペランド。 特殊化されていないテンプレートでは、Type 型の左辺値参照引数を使用します。 特殊化されたテンプレートは、推論された型 Type1 の左辺値および右辺値参照引数の完全転送を行います。

  • Right
    論理積演算の右オペランド。 特殊化されていないテンプレートでは、Type 型の左辺値参照引数を使用します。 特殊化されたテンプレートは、推論された型 Type2 の左辺値および右辺値参照引数の完全転送を行います。

戻り値

Left && Right の結果。 特殊化されたテンプレートは、結果の完全転送を行います。結果には operator&& によって返された型が含まれます。

解説

ユーザー定義型の場合、オペランドの評価のショートサーキットはありません。 どちらの引数も operator&& によって評価されます。

使用例

// functional_logical_and.cpp
// compile with: /EHsc

#define _CRT_RAND_S
#include <stdlib.h>
#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;

   unsigned int randomValue;

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

   }

   int j;
   for ( j = 0 ; j < 7 ; j++ )
   {
      if ( rand_s( &randomValue ) == 0 )
      {
         d2.push_back((bool)(( randomValue % 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 conjunction of the truth values
   // of d1 & d2, use the logical_and function object
   transform( d1.begin( ), d1.end( ), d2.begin( ),
      d3.begin( ), logical_and<bool>( ) );
   cout << "The deque which is the conjuction of d1 & d2 is:\n d3 = ( " ;
   for ( iter3 = d3.begin( ) ; iter3 != d3.end( ) ; iter3++ )
      cout << *iter3 << " ";
   cout << ")" << endl;
}
  

必要条件

ヘッダー: <functional>

名前空間: std

参照

関連項目

C++ 標準ライブラリ内のスレッド セーフ

標準テンプレート ライブラリ