'auto'作为函数参数的模板参数占位符 [英] 'auto' as a template argument placeholder for a function parameter

查看:454
本文介绍了'auto'作为函数参数的模板参数占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 20允许将auto用作函数参数类型.

它是否还允许将auto用作模板参数占位符(不相似,但是本着

可以写为:

void printPair(const std::pair<auto, auto>& p) {
    std::cout << p.first << ", " << p.second;
}

可以通过实验性GCC概念实现,并且可以很好地工作. >

在C ++ 20中是合法语法吗?

解决方案

此语法在C ++概念技术规范中有效,但在C ++ 20中无效.在C ++ 20概念中,auto仅在函数参数类型的顶层被允许.相关规则是 [dcl.spec.auto]第2段:

类型为 type-constraint [opt] auto placeholder-type-specifier 可以用作 decl-specifier 函数声明或 lambda-表达式参数声明 decl-specifier-seq 的值,如果不是 类型说明符引入了 trailing-return-type (见下文),是函数声明或通用参数类型占位符 > lambda-expression . [注意:具有通用参数类型的占位符表示该函数是缩写的函数模板(9.3.3.5 [dcl.fct])或lambda是通用lambda(7.5.5 [expr.prim.lambda]). —尾注]

(如果您在撰写本文时检查了最新工作草案中的措辞,您会发现一条稍有不同的规则.以上规则已由核心问题2447 ,一周前在布拉格委员会会议上被投票通过了C ++ 20最终草案.)

函数参数中的 decl-specifier 是参数声明开始处的关键字和类型名称的初始序列.上面的规则允许auto出现在顶层:

void f(auto x);

...,但仅作为 decl-specifier .当嵌套在 decl-specifier 中时,不允许auto:

void f(std::vector<auto> x);

...,并且在参数类型的其他地方也不允许使用

void f(void (*p)(auto));

C++20 allows using auto for function parameter type.

Does it also allow using auto as a template argument placeholder (not similar, but in the spirit of C++17 template<auto> in a way) for function parameter type?

So the following code, pre C++20:

template<typename First, typename Second>
void printPair(const std::pair<First, Second>& p) {
    std::cout << p.first << ", " << p.second;
}

Could be written as:

void printPair(const std::pair<auto, auto>& p) {
    std::cout << p.first << ", " << p.second;
}

It does compile and works nicely with experimental GCC implementation for concepts.

Is it a legitimate syntax with C++20?

解决方案

This syntax is valid in the C++ Concepts Technical Specification, but not in C++20. In C++20 concepts, auto is only permitted at the top level in a function parameter type. The relevant rule is [dcl.spec.auto] paragraph 2:

A placeholder-type-specifier of the form type-constraint[opt] auto can be used as a decl-specifier of the decl-specifier-seq of a parameter-declaration of a function declaration or lambda-expression and, if it is not the auto type-specifier introducing a trailing-return-type (see below), is a generic parameter type placeholder of the function declaration or lambda-expression. [Note: Having a generic parameter type placeholder signifies that the function is an abbreviated function template (9.3.3.5 [dcl.fct]) or the lambda is a generic lambda (7.5.5 [expr.prim.lambda]). —end note]

(If you check the wording in the most recent working draft at the time of writing, you will find a somewhat different rule. The above rule was modified by core issue 2447, which was voted into the C++20 final draft at the Prague committee meeting a week ago.)

The decl-specifiers in a function parameter are the initial sequence of keywords and type names at the start of the parameter declaration. The above rule allows auto there at the top level:

void f(auto x);

... but only as a decl-specifier. auto is not permitted when nested within a decl-specifier:

void f(std::vector<auto> x);

... and is also not permitted elsewhere in the parameter type:

void f(void (*p)(auto));

这篇关于'auto'作为函数参数的模板参数占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆