类模板部分专业化的匹配 [英] Matching of class template partial specializations

查看:121
本文介绍了类模板部分专业化的匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N4527 14.5.5.1 [temp.class.spec.match]


2部分特化与给定的实际模板参数列表匹配if

 模板<类T1,类T2,int I> class A {}; //#1 
template< class T,int I> A类 {}; //#2
template< class T1,class T2,int I> A类 {}; //#3
template< class T> A类< int,T *,5> {}; //#4
template< class T1,class T2,int I> A类 {}; //#5

A< int,int,1> a1; // uses#1
A< int,int *,1> a2; // uses#2,T is int,I is 1
A< int,char *,5> a3; // uses#4,T is char
A< int,char *,1> a4; //使用#5,T1是int,T2是char,I是1
A< int *,int *,2> a5; // ambiguous:matches#3 and#5

3也可以推导出非类型模板参数从主模板的非类型参数的实际模板参数的值。 [示例: a2 的声明。 -end example]



4在引用类模板特化的类型名称中(例如 A / code>)参数列表将
匹配主模板的模板参数列表。专业化的模板参数是从主模板的参数推导出的


在规则3中, code> I 是从第三个实际模板参数 1 中推导出来的,这是rule2说的。



它们之间有什么区别(rule2,rule3和rule4)?

另外一句话,我们已经有了rule2,rule3的意图和rule4的第二句话,为什么他们在这里?

解决方案

我认为,规则 [temp.class.spec.match] 14.5.5.1 \2 可以改写成这样而不改变其声明:



[temp.class.spec.match] 14.5.5.1 \2 strong>


部分特化与给定的实际模板参数列表
匹配,如果部分特化的模板参数可以推导
来自根据14.8.2.5的实际模板参数列表,其中P
是来自其
simple-template-id 的部分特化的参数列表,A是


规则 [temp.deduct.type] 14.8.2.5\1 定义了从类型(我不知道有关模板)的过程,所以需要规则 [temp.class.spec.match] 14.5.5.1 \3 其添加到主模板的非类型模板参数的 14.5.5.1 \2 案例,这些案例在部分专业化中没有(部分)专门化。



您在上述注释中指出的规则 [temp.class.spec.match] 14.5.5.1\4 1 2 ),只是澄清,模板参数在 template-id 对应主模板的模板参数,而不是其部分专业化可能有不同的 template-parameter-list 。此外,规则的第二句很可能声称主模板的隐式模板参数列表( 14.5.5 \4 )根据 [temp.deduct .type] 14.8.2.5 \9 。因此,短语主模板的隐式模板参数列表和专业化的模板参数意味着一个同义词,短语主模板的参数和实际模板参数列表意味着另一个同义词...但也可能是作者打算这样写:



[temp.class.spec.match] 14.5.5.1 \4 )


在引用类模板特殊化的类型名称中,参数列表应与主模板的模板参数列表匹配。部分特化的模板参数是从主模板的参数中推导出来的。

任何...


N4527 14.5.5.1[temp.class.spec.match]

2 A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list.

template<class T1, class T2, int I> class A             { }; // #1
template<class T, int I>            class A<T, T*, I>   { }; // #2
template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
template<class T>                   class A<int, T*, 5> { }; // #4
template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5

A<int, int, 1>   a1; // uses #1
A<int, int*, 1>  a2; // uses #2, T is int, I is 1
A<int, char*, 5> a3; // uses #4, T is char
A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
A<int*, int*, 2> a5; // ambiguous: matches #3 and #5

3 A non-type template argument can also be deduced from the value of an actual template argument of a non-type parameter of the primary template. [ Example: the declaration of a2 above. —end example ]

4 In a type name that refers to a class template specialization, (e.g., A<int, int, 1>) the argument list shall match the template parameter list of the primary template. The template arguments of a specialization are deduced from the arguments of the primary template.

In rule3, the example shows I is deduced from the third actual template argument 1, this is what the rule2 says. So as the second sentence of rule4, I think it is repeating what rule2 says.

What are the differences between them(rule2, rule3 and rule4)?

Another words, we already have rule2, what are the intents(meaning) of rule3 and the second sentence of rule4, why they are here?

解决方案

I think, the rule [temp.class.spec.match] 14.5.5.1\2 may be rewritten like this without changing its purport:

[temp.class.spec.match] 14.5.5.1\2 (modified)

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list according to 14.8.2.5, where P is the argument list of the partial specialization from its simple-template-id and A is the actual template argument list.

The rule [temp.deduct.type] 14.8.2.5\1 defines the process for deducing from types (I am not sure about templates though) only, so there is a need for the rule [temp.class.spec.match] 14.5.5.1\3, which adds to 14.5.5.1\2 cases with non-type template parameters of the primary template, which are not (partially) specialized in the partial specialization.

The rule [temp.class.spec.match] 14.5.5.1\4, as you noted in the comments above (1, 2), is just the clarification, that template arguments in the template-id corresponds to template parameters of the primary template, not its partial specializations, which may have different template-parameter-list s. Moreover, the second sentence of the rule most likely claims that the implicit template argument list of the primary template (14.5.5\4) is deduced (!) according to [temp.deduct.type] 14.8.2.5\9 from the actual argument list. So the phrases "implicit template argument list of the primary template" and "the template arguments of a specialization" imply one selfsame thing, and the phrases "the arguments of the primary template" and "the actual template argument list" imply another selfsame thing... But it also may be that authors intended to write this:

[temp.class.spec.match] 14.5.5.1\4 (modified)

In a type name that refers to a class template specialization the argument list shall match the template parameter list of the primary template. The template arguments of a partial specialization are deduced from the arguments of the primary template.

Whatever...

这篇关于类模板部分专业化的匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆