Const Reference in C : Placement Before vs. After Type Specifier
In C , when dealing with const references, there arises a question regarding the placement of the const keyword in relation to the type specifier: before or after? Let's examine the syntax and behavior of these two forms.
Syntax:
Const Reference Before Type Specifier:
int foo1(const Fred &arg) { ... }
Const Reference After Type Specifier:
int foo2(Fred const &arg) { ... }
Behavior:
Both syntaxes result in the same behavior. The language treats const T& and T const& as equivalent types. This applies to both pointer and reference declarations.
Stylistic Considerations:
While there is no functional difference between the two placement options, stylistic preferences vary. However, there are some arguments to be made for preferring const T& (and const T*):
The right-to-left parsing rule often cited in favor of placing const after the type specifier can be subjective. const T& can also be parsed right-to-left as "reference to a T constant."
The above is the detailed content of Const Reference in C : Before or After the Type Specifier?. For more information, please follow other related articles on the PHP Chinese website!