Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ! (Logical NOT) operator is a unary operator that performs logical negation on its operand. It evaluates the boolean state of an expression and returns the inverted boolean value: true if the operand evaluates to false, and false if the operand evaluates to true.

Syntax

!operand

Evaluation Mechanics and Type Conversion

When the ! operator is applied, the operand is contextually converted to bool before applying the negation. This specific standard terminology is critical because contextual conversions specifically allow the compiler to invoke explicit conversion operators (e.g., explicit operator bool() const), which standard implicit conversions forbid. The operator strictly returns a bool prvalue (pure rvalue).
  • Boolean Operands: !true evaluates to false; !false evaluates to true.
  • Integer and Floating-Point Types: Any non-zero value evaluates to true (resulting in false after negation). A value of exactly 0 or 0.0 evaluates to false (resulting in true after negation).
  • Pointers: A null pointer evaluates to false (resulting in true). A non-null pointer evaluates to true (resulting in false).
  • std::nullptr_t: Applying ! to nullptr evaluates to true.

Precedence and Associativity

The ! operator has high precedence. The ISO C++ Standard does not officially number precedence levels, as precedence is derived entirely from the language’s grammar rules. However, unofficial community references (such as cppreference.com) commonly group it with other unary operators like ++, --, ~, and unary - near the top of the precedence hierarchy. It features right-to-left associativity, meaning chained unary operators are evaluated from the innermost operand outward. Because its precedence is higher than relational and equality operators, parentheses are required to negate the result of a comparison:
!a == b    // Evaluates as: (!a) == b
!(a == b)  // Evaluates as: Negation of the result of (a == b)
!!a        // Evaluates as: !(!a) (Double negation, forces conversion to bool)

Operator Overloading

The ! operator can be overloaded for user-defined types by defining operator!. By convention, the overloaded operator should return a bool, though the language permits returning other types. It must be implemented as a unary function (either as a member function taking no arguments or a non-member function taking one argument). Failing to return a value from a value-returning function results in Undefined Behavior, so a return statement is mandatory. Member function signature:
class MyClass {
public:
    bool operator!() const {
        // Implementation returning boolean state
        return false; 
    }
};
Non-member function signature:
bool operator!(const MyClass& obj) {
    // Implementation returning boolean state
    return false; 
}

Alternative Token

C++ provides not as an alternative keyword for the ! operator. It functions identically at the compiler level and does not require including any headers in modern C++ (unlike C, which requires <iso646.h>).
not operand // Exactly equivalent to !operand
Master C++ with Deep Grasping Methodology!Learn More