TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
! (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
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:
!trueevaluates tofalse;!falseevaluates totrue. - Integer and Floating-Point Types: Any non-zero value evaluates to
true(resulting infalseafter negation). A value of exactly0or0.0evaluates tofalse(resulting intrueafter negation). - Pointers: A null pointer evaluates to
false(resulting intrue). A non-null pointer evaluates totrue(resulting infalse). - std::nullptr_t: Applying
!tonullptrevaluates totrue.
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:
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:
Alternative Token
C++ providesnot 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>).
Master C++ with Deep Grasping Methodology!Learn More





