简体   繁体   English

是否可以在函数模板的显式专业化中扣除多个模板参数?

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

The following quote from [temp.expl.spec.11] : 以下来自[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. 拖地的模板参数可以留在模板id未指定命名的显函数模板特殊化,只要可以从函数参数类型推断。

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. 但是,使用GCC和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 type s . 如果可以从函数参数类型s推导出显式函数模板特化, 可以在template-id中保留未指定的尾随模板参数

The "a" at the start refers to any not to one . 开头的“ a”是指任何一个都不

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. 如果可以从函数自变量类型推导出显式函数模板特化,则可以在模板ID中保留未指定的尾随模板自变量

Here's a sentence that I just made up: 这是我刚刚写的一句话:

A function parameter's type T is adjusted to const T before prior analysis. 在先前分析之前,将功能参数的类型T调整为constT

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. “ a”在广义上是指任何一件事情。

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. 如果可以从函数参数类型推导出显式函数模板特化,则在模板ID中可以不指定尾随模板参数。

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* . 我们不需要指定T1因为它可以从int推导,并且我们不需要指定T2因为它可以从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. 如果可以从函数参数类型中推导出显式函数模板特化,则可以在template-id中保留未指定的单个尾随模板参数。

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

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