我真的由模板重载/专业化解决规则的某些细节混淆。 我想通过这篇文章会得到一些关于这个问题的理解香草萨特模板超载/专业化 。 我卡在文章下面的特定点。
这里是
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
I实施例2和实施例3之间看到的唯一区别是在其中的模板b和c声明的顺序。 我不明白为什么这让一切变得不同。 什么是秩序宣言有什么关系呢? 我必须错过了C ++的基本概念,我没有看到的是,文章中的解释。
我会很感激有人清除这件事对我来说。
谢谢。