N4567 14.5.5.1 [temp.class.spec.match]p4 In a type name that refers to a class template specialization, (e.g., A) 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.
template<class T1, class T2, int I> class A { }; // #1
template<class T, int I> class A<T, T*, I> { }; // #2
A<int, int, 1> a1; // uses #1
这个“推论”是否意味着14.8.2.5 [temp.deduct.type]? Template arguments can be deduced in several different contexts, but in each case a type that is specified in terms of template parameters (call it P ) is compared with an actual type (call it A ), and an attempt is made to find template argument values (a type for a type parameter, a value for a non-type parameter, or a template for a template parameter) that will make P , after substitution of the deduced values (call it the deduced A), compatible with A.
如果是,那么P和A是什么? 专业化的模板参数是指主模板int,int,1的实际模板参数,或部分专业化的T,T *,I或其他模板参数. 主模板的参数是指主模板int,int,1的实际模板参数,还是主模板T1,T2,I或其他模板的隐式模板参数? 这句话是什么意思? 更新: @Igor Tandetnik和@R Sahu似乎有不同的答案,我需要更多帮助. 解决方法: 首先,应注意,these rules的含义更多,就好像您正在实现C解析器(如编译器)一样,因此,如果不满足这些特定规则之一,则程序应为不合格的(并产生错误) .因此,您在本段中提到: 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.
认为这意味着如果要解析的源不符合这些段落的限制,则说明它不符合要求,因此会产生错误. 直接回答以下主要问题: What does “The template arguments of a specialization are deduced from the arguments of the primary template” mean?
14.5.5是关于模板部分专业化的,14.5.5.1是专门用于匹配部分专业化的,第4段(句子的来源)只是说传递给模板的模板参数必须与专门模板的参数匹配. 第4段的最后一句话(所讨论的那个)只是说,传入的参数是根据主模板推论得出的. 它给出了一个A int,int,1的例子.在谈论本段时,它指的是14.5.5.1中给出的示例中的其他模板专业化(为便于阅读而分开): // #1 (main template)
template<class T1, class T2, int I>
class A
{ };
// #2
template<class T, int I>
class A<T, T*, I>
{ };
// #3
template<class T1, class T2, int I>
class A<T1*, T2, I>
{ };
// #4
template<class T>
class A<int, T*, 5>
{ };
// #5
template<class T1, class T2, int I>
class A<T1, T2*, I>
{ };
因此,给出以下代码: A<int, int, 1> a1;
A<int, char*, 5> a2;
A<int, int, 2.0f> a3;
A<int*> a4;
a1可以很好地编译,并且将使用模板1(因为它与该专业完全匹配),因此模板1将编译为以下内容: template<
class T1 = int,
class T2 = int,
int I = 1>
class A
{ };
a2也可以很好地编译,并且模板#4将这样使用: template<
class T = char>
class A<int, T*, 5>
{ };
但是,a3与任何模板专业都不匹配,并且合格的编译器在编译a3时会产生错误,因为int的类型与float的类型不匹配;也就是说,a3将生成以下类型的模板: template<
class T1 = int,
class T2 = int,
int I = 2.0f>
class A
{ };
由于int不是浮点数,因此应该会产生错误.最后,a4不会编译得很好,因为它只有1个模板参数,而A的所有模板专业化都带有3个参数. 继续您的问题: Does this “deduced” mean 14.8.2.5 [temp.deduct.type]?
是和否,推论是指整个14.8.2模板参数推论,其中第2段指出: When an explicit template argument list is specified, the template arguments must be compatible with the template parameter list and must result in a valid function type as described below; otherwise type deduction fails.
为了简洁起见,下面描述的其他地方未在此处发布. 但是,14.8.2.5具体涉及如何以一致的方式推导类型,如果无法以这种方式推导模板特化,则它会失败(即编译器应生成错误). If it does, what is the P and A?
这句话中的P和A只是占位符值,可用于其余文本. 具体来说,它试图获得的结果是P表示模板参数,A表示可以使用的实际类型,例如int或std :: string或用户定义的类型,例如类,struct或typedef,或功能. 以下面的代码为例: #1
template < class T >
struct A {
T val;
};
#2
template<>
struct A<double>
{
double val;
};
int main() {
A<int> a1; // uses #1
A<double> a2; // uses #2
A<someVal> a3; // uses #1 but generate error since `someVal` is invalid type
}
在此代码中,P val将是类T的模板参数,而A val对于a1来说是int,对于a2是double,对于a3是someVal.符合条件的编译器应为a3生成错误,因为在推导值(称为推导的A)替换后,没有任何类型会使P与A兼容,因为someVal不是有效类型. 使用示例A< int,int,2.0f>. A3;从上面开始,由于没有定义可以接受最后一个参数(2.0f)的模板,因此此处的P val为int,而A val为2.0f.由于2.0f不是int,因此该模板推导失败,并生成错误. 您还问: The template arguments of a specialization means the actual template arguments of the primary template int, int, 1 or the template arguments of the partial specialization T, T*, I or other?
专业化的模板自变量是指传递给模板的自变量,因此在A< int,int,1>中,此专业的模板参数为int,int和1. the arguments of the primary template means the actual template arguments of the primary template int, int, 1 or the implicitly template arguments of the primary template T1, T2, I or other?
主模板的参数指的是主模板本身,因此在上面的A示例中,主模板将是template< class T1,class T2,int I>. A类{}; 希望能对您有所帮助.
|