问题描述
在阅读时,我对以下示例感到困惑:
While reading this, I'm confused by the following examples:
// 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)
此处,(c)
是(b)
的显式专业化。
Here, (c)
is an explicit specialization of (b)
.
// 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
此处(c)
是(a)
的显式专业化。这是为什么?是因为声明的顺序吗?
Here (c)
is an explicit specialization of (a)
. Why is that? Is this because of the ordering of the declaration?
推荐答案
是的,是因为声明的顺序。当编译器在第二组中遇到(c)时,唯一定义的模板要专门化(a)。
Yes, it's because of the ordering of the declaration. When the compiler encounters (c) in the second set, the only defined template to specialize is (a).
这就是为什么在订购模板专门化项目时必须小心的原因
This is why you must be careful in ordering your template specializations.
C ++编程语言对此进行了相当详细的介绍(第13.5.1节)。我强烈推荐它。
The C++ Programming Language goes into quite some detail about this (Section 13.5.1). I highly recommend it.
这篇关于功能模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!