简体   繁体   English

现行规则草案中不参与推演的模板参数包推导为空的规范规则是什么

[英]What is the normative rule in the current draft rules that a template parameter pack which does not participate in deduction is deduced to empty

template<typename ...T, typename U>
void fun(U){}
int main(){
   fun(0);
}

This snippet code is accepted by both GCC and Clang. GCC 和 Clang 都接受此代码段代码。 The template parameter pack T does not participate in the template argument deduction in the context of function call, as per the following rules:模板形参包T不参与function调用上下文中的模板实参推演,规则如下:
[temp.deduct.call][temp.deduct.call]

Template argument deduction is done by comparing each function template parameter type (call it P) that contains template-parameters that participate in template argument deduction with the type of the corresponding argument of the call (call it A) as described below.模板参数推导是通过将包含参与模板参数推导的模板参数的每个function 模板参数类型(称为 P)与调用的相应参数的类型(称为 A)进行比较来完成的,如下所述。

The pack T is contained by any function template parameter.T包含在任何 function 模板参数中。 If there were no other special rules specify, the deduction would fail according to:如果没有其他特殊规则规定,扣除将根据以下情况失败:
[temp.deduct.type#2][temp.deduct.type#2]

if any template argument remains neither deduced nor explicitly specified, template argument deduction fails.如果任何模板参数既没有推导也没有明确指定,则模板参数推导失败。

However, such a case is ruled by the following rule in the current standard, that is: [temp.arg.explicit#4]但是,这种情况是由当前标准中的以下规则决定的,即: [temp.arg.explicit#4]

A trailing template parameter pack ([temp.variadic]) not otherwise deduced will be deduced as an empty sequence of template arguments .没有以其他方式推导的尾随模板参数包([temp.variadic])将被推导为模板 arguments 的空序列

So, the above case can be considered to deduce successfully which leaves the pack T with an empty set of template arguments.因此,上述情况可以认为成功地推导出了包T带有一个空的模板集 arguments。

However, the special rule in temp.arg.explicit#4 has been changed to a note in the current draft但是,temp.arg.explicit#4 中的特殊规则已更改为当前草案中的注释

[temp.arg.explicit#note-1] [temp.arg.explicit#note-1]

[Note 1: A trailing template parameter pack ([temp.variadic]) not otherwise deduced will be deduced as an empty sequence of template arguments. [注 1:未以其他方式推导的尾随模板参数包 ([temp.variadic]) 将推导为模板 arguments 的空序列。 — end note] ——尾注]

So, I wonder Is there any alternative normative rule in the current draft states that the pack T not otherwise deduced will be deduced to an empty set of template arguments?所以,我想知道当前草案中是否有任何替代的规范性规则规定未以其他方式推导的包T将推导出为一组空模板 arguments?

The previously normative section of [temp.arg.explicit]/4 [temp.arg.explicit]/4 之前的规范部分

[...] A trailing template parameter pack ([temp.variadic]) not otherwise deduced will be deduced as an empty sequence of template arguments. [...] 未以其他方式推导的尾随模板参数包 ([temp.variadic]) 将推导为模板 arguments 的空序列。 [...] [...]

was made into a non-normative note as part of P1787R6 .作为P1787R6的一部分,被制成非规范性注释。

As you've pointed out, as per [temp.deduct.type]/2 [ emphasis mine]:正如您所指出的,根据[temp.deduct.type]/2 [强调我的]:

Type deduction is done independently for each P / A pair [...], if any template argument remains neither deduced nor explicitly specified, template argument deduction fails.对于每个P / A对 [...],类型推导是独立完成的,如果任何模板参数既没有推导也没有明确指定,则模板参数推导失败。

[temp.arg.general] describes that a template parameter that is a template parameter pack may correspond to zero template arguments : [temp.arg.general]描述了作为模板参数包的模板参数可能对应于零模板 arguments

[...] When the parameter declared by the template is a template parameter pack, it will correspond to zero or more template-arguments . [...] 当模板声明的参数是模板参数包时,它将对应个或多个template-arguments

and [temp.variadic]/1 explicitly mention that a template parameter pack may accept zero arguments:[temp.variadic]/1明确提到模板参数包可以接受零 arguments:

A template parameter pack is a template parameter that accepts zero or more template arguments.模板参数包是接受零个或多个模板 arguments 的模板参数。

followed by a non-normative example of an empty argument list for an entity templated over a parameter pack template parameter:接下来是一个非规范示例,该示例是在参数包模板参数上模板化的实体的空参数列表:

 template<class... Types> struct Tuple { }; Tuple<> t0; // Types contains no arguments

Now, returning to [temp.arg.explicit]/4 :现在,回到[temp.arg.explicit]/4

If all of the template arguments can be deduced, they may all be omitted ;如果模板arguments都可以推导出来,则可以全部省略 in this case, the empty template argument list <> itself may also be omitted.在这种情况下,空模板参数列表<>本身也可以省略。

Meaning the Tuple example above is can likewise omit the empty argument list这意味着上面的Tuple示例同样可以省略空参数列表

Tuple t0; // Also OK: Types contains no arguments

but where the key is that, as per [temp.arg.general] above, that a template parameter list may correspond to zero arguments, in which case there are no template arguments needed to be deduced.但关键是,根据上面的 [temp.arg.general],模板参数列表可能对应于零 arguments,在这种情况下,不需要推导模板 arguments。

If you look at your own example:如果您查看自己的示例:

 template<typename...T, typename U> void fun(U){} int main(){ fun(0); // #1 }

you could likewise invoke #1 as:你也可以调用#1

fun<>(0);  // argument list for parameter pack is empty
           // -> no argument (beyond that for `U`) to deduce

highlighting that the deducible argument corresponding to the template parameter U can be omitted from the explicit template-arguments, whereas the remaining template-arguments are none ;强调与模板参数U对应的可推导参数可以从显式模板参数中省略,而剩余的模板参数是 none namely, the argument list for the template parameter that is a template parameter pack is empty , and there are thus no remaining template arguments that needs to be deduced.即作为模板参数包的模板参数的参数列表为,因此没有剩余的模板arguments需要推导。

Thus因此

A trailing template parameter pack ([temp.variadic]) not otherwise deduced will be deduced as an empty sequence of template arguments.未以其他方式推导的尾随模板参数包 ([temp.variadic]) 将推导为模板 arguments 的空序列。

is non-normative/redundant, explaining why it was wrapped into a [Note - [...] - end note] .是非规范/冗余的,解释了为什么它被包装成[Note - [...] - end note]

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

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