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

Viewed 90

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 )

So are the following equivalent as far as instantiation, const types, reference types, etc?

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

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

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. 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.

Related