Passing Auto as an Argument in C
There exists a method to utilize the "auto" keyword as an argument when passing it to another function. That is to say, you can define a function like this:
int function(auto data) { // Does something }
C 20's Introduction of Auto as a Function Parameter Type
C 20 enables the usage of "auto" as a function parameter type. Consider the following code snippet as an example:
int function(auto data) { // Do something, there are no constraints on data }
Abbreviated Function Templates
Additionally, "auto" can be employed as an abbreviated function template. This is a specialized instance of a non-constraining type constraint, or, in other words, an "unconstrained auto parameter." A "constrained auto parameter," on the other hand, would employ concepts and appear as follows:
void function(const Sortable auto& data) { // Do something that requires data to be Sortable // Assuming there is a concept named Sortable }
Explanation from the C Specification
The C specification defines "placeholder type specifiers" as follows:
type-constraint<sub>opt</sub> auto type-constraint<sub>opt</sub> decltype ( auto )
Such placeholder-type-specifiers designate a placeholder type that is subject to later replacement through deduction from an initializer. When used in the "decl-specifier-seq" of a function declaration or lambda expression, a placeholder-type-specifier of the form "type-constraintopt auto" indicates that the function is an abbreviated function template (as per 9.3.3.5).
The above is the detailed content of Can C Functions Utilize \'auto\' as an Argument Type?. For more information, please follow other related articles on the PHP Chinese website!