auto as a template argument placeholder for a function parameter

谁说我不能喝 提交于 2020-03-14 05:41:27

问题


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?


回答1:


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));


来源:https://stackoverflow.com/questions/60358154/auto-as-a-template-argument-placeholder-for-a-function-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!