需要了解函数模板解析规则 [英] Need to understand function template resolution rules

查看:158
本文介绍了需要了解函数模板解析规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑的模板重载/专业化解析规则的某些细节。我试图通过阅读这篇文章,通过 Herb Sutter对模板重载/专业化, / a>。

I'm really confused by certain details of template overloading/specialization resolution rules. I tried to get some understanding on the subject by going through this article by Herb Sutter on template overloading/specialization. I'm stuck on the following specific point in the article.

这是

Consider the following code:
// Example 2: Explicit specialization 
// 
template<class T> // (a) a base template 
void f( T );
template<class T> // (b) a second base template, overloads (a) 
void f( T* );     //     (function templates can't be partially 
                  //     specialized; they overload instead)
template<>        // (c) explicit specialization of (b) 
void f<>(int*);
// ...
int *p; 
f( p );           // calls (c)
The result for the last line in Example 2 is just what you'd expect. The question of the day, however, is why you expected it. If you expected it for the wrong reason, you will be very surprised by what comes next. After all, "So what," someone might say, "I wrote a specialization for a pointer to int, so obviously that's what should be called" - and that's exactly the wrong reason.
Consider now the following code, put in this form by Peter Dimov and Dave Abrahams:
// Example 3: The Dimov/Abrahams Example 
// 
template<class T> // (a) same old base template as before 
void f( T );
template<>        // (c) explicit specialization, this time of (a)
void f<>(int*);
template<class T> // (b) a second base template, overloads (a) 
void f( T* );
// ...
int *p; 
f( p );           // calls (b)! overload resolution ignores 
                  // specializations and operates on the base 
                  // function templates only

我在示例2和示例3之间看到的唯一区别是模板b和c的声明顺序。我不明白为什么会使所有的区别。声明的顺序是什么?我必须错过了c ++的基本概念,我在文章中没有看到这一点的解释。

The only difference I see between Example 2 and Example 3 is the order in which the templates b and c are declared. I don't understand why that makes all the difference. What's order of declaration got to do with anything? I must have missed a basic concept of c++, and I don't see an explanation for that in the article.

我真的很感谢有人为我清除这一点。

I'd really appreciate somebody clearing this up for me.

感谢。

推荐答案


What's order of declaration got to do with anything?

专业化声明必须始终遵循正在专门化的模板的声明(14.7.3 / 3)。在示例2中,(c)是两者(a)和(b)的显式专门化。在示例3中,(c)仅是(a)的显式特化,因为(b)跟在后面。

The declaration of a specialization must always follow the declaration of the template which is being specialized (14.7.3/3). In Example 2, (c) is an explicit specialization of both (a) and (b). In Example 3, (c) is only an explicit specialization of (a), since (b) follows it.

当执行重载分辨率时,无论模板是否具有明确的专业化。在这两个示例中,重载分辨率选择模板(b)over(a),因为它更专门(14.5.6.2)。在示例2中,选择哪一个实际上并不重要,因为(c)是两个模板的特殊化,因此无论什么都被调用。在示例3中,它是重要的。因为(b)胜过重载分辨率并且(c)不是(b)的特殊化,所以(c)不被调用。

When overload resolution is being performed, it doesn't matter whether a template has explicit specializations or not. In both examples, overload resolution selects the template (b) over (a) because it's more specialized (14.5.6.2). In Example 2 it doesn't actually matter which one is selected, because (c) is a specialization of both templates, so it gets called no matter what. In Example 3, it does matter. Because (b) wins overload resolution and (c) is not a specialization of (b), it follows that (c) doesn't get called.

这篇关于需要了解函数模板解析规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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