有没有办法在 C++ 中将 auto 作为参数传递? [英] Is there a way to pass auto as an argument in C++?
问题描述
有没有办法将 auto 作为参数传递给另一个函数?
int 函数(自动数据){//做某事}
C++20 允许 auto
作为函数参数类型
此代码在使用 C++20 时有效:
int 函数(自动数据){//做某事,对数据没有约束}
作为缩写函数模板.
这是非约束类型约束的特例(即无约束自动参数).使用概念,约束类型约束版本(即约束自动参数)将是例如:
void function(const Sortable auto& data) {//做一些需要数据可排序的事情//假设有一个名为 Sortable 的概念}
规范中的措辞,在我朋友的帮助下Yehezkel Bernat:
<块引用>9.2.8.5 占位符类型说明符 [dcl.spec.auto]
占位符类型说明符:
<块引用>类型约束opt 自动
type-constraintopt decltype ( auto )
<块引用>占位符类型说明符指定一个占位符类型,稍后将通过从初始化程序.
表单的占位符类型说明符type-constraintopt auto 可用于 a 的 decl-specifier-seq函数声明或 lambda 表达式的参数声明并表示该函数是一个缩写的函数模板(9.3.3.5) ...
Is there a way to pass auto as an argument to another function?
int function(auto data)
{
//DOES something
}
C++20 allows auto
as function parameter type
This 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) ...
这篇关于有没有办法在 C++ 中将 auto 作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!