Although extra parentheses generally don't alter the meaning of a C program, there are specific situations where they do have an effect, beyond overriding basic operator precedence:
Parentheses around function calls, such as (fn)(arg), prevent argument-dependent name lookup (ADL). Without parentheses, fn(arg) would search in enclosing namespaces for possible matching functions.
In list contexts like function arguments or initializer lists, a, (b, c), d allows the comma operator to apply to (b, c) separately, unlike the standard a, b, c, d notation.
Extra parentheses can disambiguate code where a declaration-like syntax clashes with a function or expression statement. For example, in S w(int(a)); and S y((int)a);, parentheses make it clear they are object declarations, not function declarations.
decltype(e) deduces whether the operand is an lvalue or rvalue reference. Adding parentheses, as in decltype((e)), forces the operand to be treated as an rvalue reference.
Parentheses can be used within macro definitions to avoid operator precedence issues, protect macro arguments with commas, and prevent macro expansion in headers. For example, #define TIMES(A, B) (A) * (B); ensures that TIMES(1 2, 2 1) evaluates to 9.
The above is the detailed content of When and How to Use Extra Parentheses in C to Manipulate Name Lookup, Comma Operator Behavior, or Expression Evaluation?. For more information, please follow other related articles on the PHP Chinese website!