简体   繁体   English

自动占位符类型和显式模板类型在 C++20 中是否等效?

[英]Are auto placeholder types and explicit template types equivalent in C++20?

Are auto placeholder types and explicitly defined template type parameters completely equivalent?自动占位符类型和显式定义的模板类型参数是否完全等效?

I've read that the spec states that "A placeholder-type-specifier designates a placeholder type that will be replaced later by deduction from an initializer."我读过规范声明“占位符类型说明符指定一个占位符类型,稍后将通过初始化程序的推导来替换它。”

(9.2.8.5 Placeholder type specifiers http://eel.is/c++draft/dcl.spec.auto#:auto ) (9.2.8.5 占位符类型说明符http://eel.is/c++draft/dcl.spec.auto#:auto

So are the following equivalent as far as instantiation, const types, reference types, etc?那么就实例化、const 类型、引用类型等而言,以下等效项是什么?

void f1(auto param) {
     using T = decltype(param);
     T t1;
}

template <class T> void f2(T param ) {
     T t1;
}

They are functionally equivalent, but it's important to note that the standard does not declare them to be actually equivalent.它们在功能上是等价的,但重要的是要注意标准并未声明它们实际上是等价的。 That is, f1 and f2 work in virtually every way the same.也就是说, f1f2几乎以相同的方式工作。 But you could never do this:但你永远不能这样做:

void f1(auto); //declaration of a template.

template<typename T>
void f1(T t) //definition of a template.
{...}

The declaration of f1 does not match the definition of f1 . f1的声明与f1定义不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM