Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Logical operators

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       

      Returns the result of a boolean operation.

      Operator nameSyntaxOver​load​ablePrototype examples (forclass T)
      Inside class definitionOutside class definition
      negationnot a

      !a

      Yesbool T::operator!()const;bool operator!(const T&a);
      ANDa and b

      a&& b

      Yesbool T::operator&&(const T2&b)const;bool operator&&(const T&a,const T2&b);
      inclusive ORa or b

      a|| b

      Yesbool T::operator||(const T2&b)const;bool operator||(const T&a,const T2&b);
      Notes
      • The keyword-like forms (and,or,not) and the symbol-like forms (&&,||,!) can be used interchangeably (seealternative representations).
      • All built-in operators returnbool, and mostuser-defined overloads also returnbool so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (includingvoid).
      • Builtin operators&& and|| perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands.

      Contents

      [edit]Explanation

      The logic operator expressions have the form

      !rhs (1)
      lhs&&rhs (2)
      lhs||rhs (3)
      1) Logical NOT
      2) Logical AND
      3) Logical inclusive OR

      If the operand is notbool, it is converted tobool usingcontextual conversion to bool: it is only well-formed if the declarationbool t(arg) is well-formed, for some invented temporaryt.

      The result is abool prvalue.

      For the built-in logical NOT operator, the result istrue if the operand isfalse. Otherwise, the result isfalse.

      For the built-in logical AND operator, the result istrue if both operands aretrue. Otherwise, the result isfalse. This operator isshort-circuiting: if the first operand isfalse, the second operand is not evaluated.

      For the built-in logical OR operator, the result istrue if either the first or the second operand (or both) istrue. This operator is short-circuiting: if the first operand istrue, the second operand is not evaluated.

      Note thatbitwise logic operators do not perform short-circuiting.

      [edit]Results

      atruefalse
      !afalsetrue
      anda
      truefalse
      btruetruefalse
      falsefalsefalse
      ora
      truefalse
      btruetruetrue
      falsetruefalse

      Inoverload resolution against user-defined operators, the following built-in function signatures participate in overload resolution:

      bool operator!(bool)
      bool operator&&(bool,bool)
      bool operator||(bool,bool)

      [edit]Example

      Run this code
      #include <iostream>#include <sstream>#include <string> int main(){int n=2;int* p=&n;// pointers are convertible to boolif(    p&&*p==2// "*p" is safe to use after "p &&"||!p&&  n!=2)// || has lower precedence than &&std::cout<<"true\n"; // streams are also convertible to boolstd::stringstream cin;    cin<<"3...\n"<<"2...\n"<<"1...\n"<<"quit";std::cout<<"Enter 'quit' to quit.\n";for(std::string line;std::cout<<"> "&&std::getline(cin, line)&& line!="quit";)std::cout<< line<<'\n';}

      Output:

      trueEnter 'quit' to quit.> 3...> 2...> 1...>

      [edit]Standard library

      Because the short-circuiting properties ofoperator&& andoperator|| do not apply to overloads, and because types with boolean semantics are uncommon, only two standard library classes overload these operators:

      applies a unary arithmetic operator to each element of the valarray
      (public member function ofstd::valarray<T>)
      applies binary operators to each element of two valarrays, or a valarray and a value
      (function template)
      checks if an error has occurred (synonym offail())
      (public member function ofstd::basic_ios<CharT,Traits>)[edit]

      [edit]See also

      Operator precedence

      Operator overloading

      function object implementingx&& y
      (class template)[edit]
      function object implementingx|| y
      (class template)[edit]
      function object implementing!x
      (class template)[edit]
      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      a+= b
      a-= b
      a*= b
      a/= b
      a%= b
      a&= b
      a|= b
      a^= b
      a<<= b
      a>>= b

      ++a
      --a
      a++
      a--

      +a
      -a
      a+ b
      a- b
      a* b
      a/ b
      a% b
      ~a
      a& b
      a| b
      a^ b
      a<< b
      a>> b

      !a
      a&& b
      a|| b

      a== b
      a!= b
      a< b
      a> b
      a<= b
      a>= b
      a<=> b

      a[...]
      *a
      &a
      a->b
      a.b
      a->*b
      a.*b

      function call

      a(...)
      comma

      a, b
      conditional

      a? b: c
      Special operators

      static_cast converts one type to another related type
      dynamic_cast converts within inheritance hierarchies
      const_cast adds or removescv-qualifiers
      reinterpret_cast converts type to unrelated type
      C-style cast converts one type to another by a mix ofstatic_cast,const_cast, andreinterpret_cast
      new creates objects with dynamic storage duration
      delete destructs objects previously created by the new expression and releases obtained memory area
      sizeof queries the size of a type
      sizeof... queries the size of apack(since C++11)
      typeid queries the type information of a type
      noexcept checks if an expression can throw an exception(since C++11)
      alignof queries alignment requirements of a type(since C++11)

      C documentation forLogical operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/operator_logical&oldid=172240"

      [8]ページ先頭

      ©2009-2025 Movatter.jp