Extra Parentheses in C Beyond Operator Precedence
While extra parentheses generally do not affect program behavior in C , there are specific contexts where they can alter the meaning of the code.
Preventing Argument-Dependent Name Lookup (ADNL)
Using parentheses around a function call, (fun)(arg), prevents argument-dependent name lookup. This means that namespace-scope friend functions or function template declarations that would otherwise be considered based on the argument types will not be included in the lookup.
Enabling Comma Operator in List Contexts
In list-like contexts such as function arguments and initializer lists, parentheses can enable the use of the comma operator where it would not apply in the regular form. For example, a, (b, c), d will have the comma operator applied to b and c, whereas a, b, c, d would not.
Ambiguity Resolution of Vexing Parses
Extra parentheses can resolve parsing ambiguities in certain declarations. The "Most Vexing Parse" is a famous example where a declaration can be误interpreted as a function if extra parentheses are not present.
Deduction of Reference in decltype Expressions
In decltype expressions, the presence or absence of parentheses affects the deduced referenceness. For example, decltype(e) deduces the type of e, while decltype((e)) deduces the type of the expression (e).
Preventing Preprocessor Macro Related Errors
Extra parentheses can prevent preprocessor macro related errors. For example, enclosing macro parameters in parentheses #define TIMES(A, B) (A) * (B); ensures that the operator precedence is correct.
The above is the detailed content of Do Extra Parentheses in C Affect Program Behavior?. For more information, please follow other related articles on the PHP Chinese website!