Is there a way to pass auto as an argument to another function?
int function(auto data)
{
//DOES something
}
auto
as function parameter typeThis code is valid using C++20:
int function(auto data) {
// do something, there is no constraint on data
}
As an abbreviated function template.
This is a special case of a non constraining type-constraint (i.e. unconstrained auto parameter). Using concepts, the constraining type-constraint version (i.e. constrained auto parameter) would be for example:
void function(const Sortable auto& data) {
// do something that requires data to be Sortable
// assuming there is a concept named Sortable
}
The wording in the spec, with the help of my friend Yehezkel Bernat:
9.2.8.5 Placeholder type specifiers [dcl.spec.auto]
placeholder-type-specifier:
type-constraintopt auto
type-constraintopt decltype ( auto )
A placeholder-type-specifier designates a placeholder type that will be replaced later by deduction from an initializer.
A placeholder-type-specifier of the form type-constraintopt auto can be used in the decl-specifier-seq of a parameter-declaration of a function declaration or lambda-expression and signifies that the function is an abbreviated function template (9.3.3.5) ...