Daniel Langr
Daniel Langr

Reputation: 23497

Is deduction of multiple template arguments in an explicit specialization of a function template allowed?

The following quote from [temp.expl.spec.11]:

A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type.

indicates that only a single trailing template argument may be deduced. Which would make the following exemplary code incorrect:

template <typename T1, typename T2>
void f(T1, T2*);  

template<>
void f(int, double*) { }

int main()
{
    auto d = 2.0;
    f(1, &d);
}

However, the code compiles fine with GCC and Clang. Do these compilers apply some non-standard language extension, or is the deduction supported for multiple trailing arguments?

If the latter is true, why the sentence is not formed as follows?

Trailing template-arguments can be left unspecified in the template-id naming an explicit function template specialization provided they can be deduced from the function argument types.

Upvotes: 2

Views: 208

Answers (2)

NathanOliver
NathanOliver

Reputation: 180585

The quote does not indicate only a single parameter can be left off.

A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type.

Means that a(any) parameter is allowed to be left off if it can be deduced. So in

template<>
void f(int, double*) { }

We don't need to specifiy T1 because it can be deduced from int, and we don't need to specify T2 because it can be deducedfrom double*.

If the standard only allowed a single parameter to not be specified it would word it like

A single trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type.

Upvotes: 3

Rakete1111
Rakete1111

Reputation: 48948

The "a" at the start refers to any not to one.

A[ny] trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type.

Here's a sentence that I just made up:

A function parameter's type T is adjusted to const T before prior analysis.

This doesn't mean that only one of the many parameters is adjusted, but every one of them; if any, since it could also be that there are no parameters.

The "a" refers in a more general sense to any one thing.

Upvotes: 3

Related Questions