何时提供默认参数作为模板参数 [英] When to supply default arguments as template arguments
问题描述
template<typename T, typename U = T>
struct Test{};
template<typename T>
void func(Test<T>){ //#1
}
int main(){
func(Test<int>{}); //#2
}
在标准中似乎没有规则提到模板参数需要默认参数的情况.
It seems to no rule in the standard that mentioned what situation the default argument is required for template-parameter.
如果在参数声明中指定了初始化子句,则将该初始化子句用作默认参数.默认参数将用于呼叫缺少尾部参数的情况.
在本节中,规则明确描述了何时为函数调用提供默认参数.但是,我在标准中没有找到与上述句子相似的引语,该引语描述了何时提供默认参数作为模板参数.
In this section, the rule explicitly describe when to supply default arguments for a function call. However I haven't found a quote in the standard similar with the above sentence that describes when to supply default arguments as template arguments.
例如#1
上的 Test< T>
.也许在#1
处,专业化将是 Test< T,T>
,这只是推断.但是,标准中没有正式的术语对此进行明确描述.
Such as Test<T>
at #1
. Maybe at #1
, the specialization would be Test<T,T>
, It's just inference. However, there's no formally terminology explicitly describe this in the standard.
将默认参数作为模板参数的唯一引用是在以下规则中:
temp#arg-8
The only quote that implies the default arguments as template arguments is in the following rule:
temp#arg-8
当simple-template-id未命名函数时,默认模板参数会在需要该默认参数的值时隐式实例化.[示例:
template<类型名T,类型名U = int>struct S {};
S< bool> p;//p的类型为S< bool,int> *
U的默认参数是实例化的类型 S< bool,int> *.—示例]
When a simple-template-id does not name a function, a default template-argument is implicitly instantiated when the value of that default argument is needed. [ Example:
template<typename T, typename U = int> struct S { };
S<bool>* p; // the type of p is S<bool, int>*
The default argument for U is instantiated to form the type S<bool, int>*. — end example ]
考虑#1
处的代码,默认参数是否需要#1
处的代码?如果是这样(这似乎很必要,因为如果我没有为模板参数U指定默认参数,那么在#1
处将发生错误.请参见 godbolt结果),根据以上引用,默认模板参数需要隐式实例化>,但是,此时, T
是模板参数,此类功能模板的定义不会实例化任何内容(此时仅是功能模板定义).那么,报价如何解释呢?
Consider the code at #1
, Does the default argument need at #1
? If it is(It seems to It need at that point because If I don't specify a default argument for template parameter U, then at the point #1
will occur an error. see godbolt outcome), According to the above quote, the default template-argument need to be implicitly instantiated, However at this point, T
is a template-parameter and the definition of such function template instantiate nothing(It's just a function template definition at this point). So, How does the quote interpret this?
推荐答案
在 [临时名称] :
模板ID是否有效
A template-id is valid if
- 每个不可推导的非压缩参数都有一个参数,该参数没有默认的模板参数,
因此,可以考虑需要一个 default-argument ,这意味着需要一个default-argument才能使template-id有效,并且当缺少template参数时,该参数为默认参数值.但是我还没有在类模板的标准中找到任何明确的内容.对于功能模板,更多的是明确.可能没有人指出标准中的这个漏洞,因为这是一个常见的模式:使用 default代替用户未提供的内容.也许不会更改,因为已经给出了英语词典中default的定义:
So one could consider that a default-argument is needed means a default-argument is needed in order to make the template-id valid and that when there lacks a template argument, the argument is the default argument value. But I have not found anything explicit in the standard for class templates. For function template this is more explicit. Probably nobody pointed out this hole in the standard because this is a common pattern: default is used in place of what is not user provided. Maybe it will not be changed because the definition of default in the english dictionary is already given:
IT.如果您没有做出其他不同的选择,那么事情发生或自动出现的方式,尤其是在计算机上- cambrige在线词典
这篇关于何时提供默认参数作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!