“专业化的模板参数是从主模板的参数推导出的"是什么?意思是? [英] What does "The template arguments of a specialization are deduced from the arguments of the primary template" mean?

查看:36
本文介绍了“专业化的模板参数是从主模板的参数推导出的"是什么?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N4567 14.5.5.1 [temp.class.spec.match] p4

N4567 14.5.5.1 [temp.class.spec.match]p4

在引用类模板专业化的类型名称中(例如A),参数列表应 与主模板的模板参数列表匹配. 专业化的模板参数是从主模板的参数推导出的.

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]?

Does this "deduced" mean 14.8.2.5 [temp.deduct.type]?

可以在几种不同的上下文中推导出模板参数,但是在每种情况下,将根据模板参数指定的类型(称为P)与实际类型(称为A)进行比较,并且尝试寻找在替换推论值后将生成P的模板参数值(类型参数的类型,非类型参数的值或模板参数的模板)推导出A),与A兼容.

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是什么?

If it does, what is the P and A?

专业化的模板参数表示 主要模板实际模板参数 int, int, 1 部分专业化 T, T*, I或其他模板变量?

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?

主模板的参数表示主模板的实际模板参数 int, int, 1 主模板的隐式模板参数 T1, T2, I还是其他?

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?

这句话是什么意思?

更新:

@Igor Tandetnik和@R Sahu似乎有不同的答案,我需要更多帮助.

It looks @Igor Tandetnik and @R Sahu have different answers, I need more help.

推荐答案

首先,应注意这些规则的含义更像是在实现C ++解析器(如编译器),因此,如果不满足这些特定规则之一,则程序应不符合规范(并生成错误).因此,在该段中您提到:

First, it should be noted that these rules are meant more as if you were implementing a C++ parser (like a compiler), so if one of these specific rules is not met, then the program should be non-conforming (and an error generated). So in the paragraph you mention:

在引用类模板专业化的类型名称中(例如,A<int, int, 1>),参数列表应与主模板的模板参数列表匹配.专业化的模板参数是从主模板的参数推导出来的.

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.

认为这意味着如果所解析的源不符合这些段落的限制,则说明该源不符合要求,因此会产生错误.

Think of it to mean that if the source being parsed does not meet the restrictions of these paragraphs, it is non-conforming so generate an error.

直接回答以下主要问题:

Directly answering the main question of:

从主模板的参数推导出专业化的模板参数"是什么意思?

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段(句子的来源)只是说传递给模板的模板参数必须与专门模板的参数匹配.

14.5.5 is about template partial specializations, 14.5.5.1 is specifically about matching the partial specialization and paragraph 4 (where the sentence is from) is just saying that the template arguments passed to a template must match those of a specialized template.

第4段的最后一句话(所讨论的那个)只是说,传入的参数是根据主模板推论得出的.

The last sentence of paragraph 4 (the one in question), is simply saying that the arguments passed in are deduced based on the main template.

在谈论本段时,它给出了A<int, int, 1>的示例,并且它引用了在14.5.5.1中给出的示例中的其他模板专业化(为便于阅读而分开):

It gives an example of A<int, int, 1> when talking about this paragraph, and it's referring to the other template specializations in the example it gives in 14.5.5.1 (separated for easier reading):

// #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将编译为以下内容:

a1 will compile fine, and template #1 will be used (since it matches exactly to that specialization), so template #1 will compile to the following:

template<
    class T1 = int,
    class T2 = int,
    int I = 1>
class A
{ };

a2也可以很好地编译,并且模板#4可以这样使用:

a2 will compile fine as well and template #4 will be used as such:

template<
    class T = char>
class A<int, T*, 5>
{ };

但是,

a3与任何模板专业都不匹配,并且由于int的类型与float的类型不匹配,因此合格的编译器在编译a3时将产生错误.也就是说,a3会生成以下类型的模板:

a3, however, doesn't match any of the template specializations, and a conforming compiler will generate an error when compiling a3 since the types of int do not match type of float; that is, a3 would generate a template of the following type:

template<
    class T1 = int,
    class T2 = int,
    int I = 2.0f>
class A
{ };

,由于int不是float,因此应该会产生错误.最后,a4也不会编译好,因为它只有1个模板参数,并且A的所有模板专业化都带有3个参数.

And thus should generate an error since an int is not a float. Lastly, a4 will not compile as well since it only has 1 template argument and all template specializations for A take 3 arguments.

继续回答您的问题:

此推论"是否表示14.8.2.5 [temp.deduct.type]?

Does this "deduced" mean 14.8.2.5 [temp.deduct.type]?

是,不是,deduced是指整个14.8.2 Template argument deduction,其中第2段指出:

Yes and no, deduced is referring to the whole of 14.8.2 Template argument deduction, where paragraph 2 states:

指定显式模板参数列表时,模板参数必须与模板参数列表兼容,并且必须产生有效的函数类型,如下所述;否则类型推导将失败.

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.

为简便起见,described below还有其他要点未在此处发布.

Where described below has additional points not posted here for brevities sake.

但是,14.8.2.5具体指的是如何以一致的方式推导类型,并且如果无法以这种方式推导模板特化,则它应该失败(即编译器应生成一个错误).

However, 14.8.2.5 specifically refers to how to deduce the type in a conforming way, and if a template specialization can't be deduced this way then it should fail (i.e. compiler should generate an error).

如果是,那么P和A是什么?

If it does, what is the P and A?

此句子中的PA只是占位符值,用于其余文本.

The P and A in this sentence are just place holder values to use for the rest of the text.

具体来说,它试图获得的是P表示模板参数,而A表示可以使用的实际类型,例如intstd::string或用户定义的类型例如classstructtypedef或函数.

Specifically what it's trying to get at is that P is to mean a template argument and A is to mean an actual type that can be used, like an int or std::string or a user defined type like a class, struct or typedef, or function.

以下面的代码为例:

#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将是class T的模板参数,而A val将对于a1int,对于a2double对于a2someVal c17>.符合标准的编译器应该为a3生成错误,因为没有类型that will make P, after substitution of the deduced values (call it the deduced A), compatible with A,因为someVal不是有效类型.

In this code, the P val would be the template argument of class T and the A val would be int for a1, double for a2 and someVal for a3. A conforming compiler should generate an error for a3 since there is not type that will make P, after substitution of the deduced values (call it the deduced A), compatible with A, since someVal is not a valid type.

使用上面的示例A<int, int, 2.0f> a3;,因为没有定义可以接受最后一个参数(2.0f)的模板,所以此处的P值是intA值是;由于2.0f不是int,因此此模板推导失败并生成错误.

Using the example A<int, int, 2.0f> a3; from above, since there were no templates defined that could take the last argument (the 2.0f), the P val here is int and the A val is 2.0f; since 2.0f is not an int, this template deduction fails and an error is generated.

您还问:

专业化的模板参数是指主模板int,int,1的实际模板参数,还是部分专业化的T,T *,I或其他模板的模板参数?

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?

The template arguments of a specialization是指传递给模板的参数,因此在A<int, int, 1>中,此专业化的模板参数为intint1.

The template arguments of a specialization is referring to the arguments passed into the template, so in A<int, int, 1> the template arguments of this specializations are int, int and 1.

主模板的参数是指主模板int,int,1的实际模板参数,还是主模板T1,T2,I或其他模板的隐式模板参数?

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?

the arguments of the primary template指的是主要模板本身,因此在上面的A示例中,主要模板将是template<class T1, class T2, int I> class A { };

the arguments of the primary template are referring to the primary template itself, so in the A example above, the primary template would be template<class T1, class T2, int I> class A { };

我希望能对您有所帮助.

I hope that can help.

这篇关于“专业化的模板参数是从主模板的参数推导出的"是什么?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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