模板特化允许为特定类型或条件提供定制化的模板实现,是 静态多态(Static Polymorphism) 的重要机制。通过特化,可以优化性能、处理特殊类型或限制模板的适用范围。以下结合代码和底层原理,全面解析模板特化的核心概念。
// 通用模板
template <typename T>
class Printer {
public:
void print() { cout << "Generic Printer" << endl; }
};
// 全特化(针对 int)
template <>
class Printer<int> {
public:
void print() { cout << "Int Printer" << endl; }
};
int main() {
Printer<double> p1; // 使用通用模板
p1.print(); // 输出 "Generic Printer"
Printer<int> p2; // 使用全特化版本
p2.print(); // 输出 "Int Printer"
return 0;
}
Printer
→ _ZN7PrinterIiE5printEv
。// 通用模板
template <typename T>
void process(T value) {
cout << "Generic process: " << value << endl;
}
// 全特化(针对 double)
template <>
void process<double>(double value) {
cout << "Specialized for double: " << value << endl;
}
int main() {
process(10); // 调用通用模板(T = int)
process(3.14); // 调用全特化版本(T = double)
return 0;
}
)。// 通用模板
template <typename T, typename U>
class Pair {
public:
void describe() { cout << "Generic Pair" << endl; }
};
// 偏特化:当两个类型相同时
template <typename T>
class Pair<T, T> {
public:
void describe() { cout << "Same Type Pair" << endl; }
};
// 偏特化:当第二个类型为指针时
template <typename T, typename U>
class Pair<T, U*> {
public:
void describe() { cout << "Pointer Pair" << endl; }
};
int main() {
Pair<int, double> p1; // 通用模板
p1.describe(); // 输出 "Generic Pair"
Pair<int, int> p2; // 偏特化(相同类型)
p2.describe(); // 输出 "Same Type Pair"
Pair<int, double*> p3; // 偏特化(指针类型)
p3.describe(); // 输出 "Pointer Pair"
return 0;
}
偏特化可以基于参数类型(如指针、引用)或常量性进行约束:
template <typename T>
class Checker {
public:
void check() { cout << "Non-const T" << endl; }
};
template <typename T>
class Checker<const T> {
public:
void check() { cout << "Const T" << endl; }
};
int main() {
Checker<int> c1;
c1.check(); // 输出 "Non-const T"
Checker<const int> c2;
c2.check(); // 输出 "Const T"
return 0;
}
template <typename T> class Widget; // 通用模板
template <typename T> class Widget<T*>; // 偏特化(指针)
template <> class Widget<int*>; // 全特化(int 指针)
Widget<double*> w1; // 匹配偏特化(指针)
Widget<int*> w2; // 匹配全特化
Widget
全特化 → _ZN6WidgetIiEC1Ev
Widget
偏特化 → _ZN6WidgetIPdEC1Ev
bool
)优化存储(如位向量)。static_assert
在通用模板中禁止某些类型,并在特化中允许。特性 | 全特化 | 偏特化 |
---|---|---|
语法 | template<> class C |
template |
适用范围 | 完全具体类型 | 部分参数或类型约束 |
优先级 | 最高 | 介于全特化和通用模板之间 |
函数模板 | 支持 | 不支持 |
以下代码的输出是什么?
template <typename T>
class Processor {
public:
void process() { cout << "Generic Processor" << endl; }
};
template <typename T>
class Processor<T*> {
public:
void process() { cout << "Pointer Processor" << endl; }
};
template <>
class Processor<int*> {
public:
void process() { cout << "Int Pointer Processor" << endl; }
};
int main() {
Processor<double*> p1;
Processor<int*> p2;
p1.process();
p2.process();
return 0;
}
A. Pointer Processor
和 Int Pointer Processor
B. Generic Processor
和 Int Pointer Processor
C. Pointer Processor
和 Generic Processor
D. 编译失败,存在歧义
以下代码的输出是什么?
template <typename T>
void print(T val) { cout << "Template: " << val << endl; }
template <>
void print<int>(int val) { cout << "Specialized: " << val << endl; }
void print(int val) { cout << "Overload: " << val << endl; }
int main() {
print(10); // 调用哪个版本?
print(3.14); // 调用哪个版本?
return 0;
}
A. Specialized:
和 Template:
B. Overload:
和 Template:
C. Specialized:
和 Overload:
D. 编译失败,存在歧义
以下代码的输出是什么?
template <typename T>
class Checker {
public:
void check() { cout << "Non-const T" << endl; }
};
template <typename T>
class Checker<const T> {
public:
void check() { cout << "Const T" << endl; }
};
int main() {
Checker<int> c1;
Checker<const double> c2;
c1.check();
c2.check();
return 0;
}
A. Non-const T
和 Non-const T
B. Non-const T
和 Const T
C. Const T
和 Const T
D. 编译失败,偏特化语法错误
以下代码的输出是什么?
template <typename T>
class Counter {
public:
static int count;
Counter() { count++; }
};
template <typename T>
int Counter<T>::count = 0;
template <>
class Counter<int> {
public:
static int count;
Counter() { count += 2; }
};
int Counter<int>::count = 0;
int main() {
Counter<double> a, b;
Counter<int> c, d;
cout << Counter<double>::count << " " << Counter<int>::count << endl;
return 0;
}
A. 2 4
B. 2 2
C. 2 0
D. 0 4
以下代码的输出是什么?
template <typename T>
class Base {
public:
void print() { cout << "Base" << endl; }
};
template <>
class Base<int> {
public:
void print() { cout << "Base" << endl; }
};
class Derived : public Base<int> {
public:
void print() { cout << "Derived" << endl; }
};
int main() {
Derived d;
d.print();
return 0;
}
A. Base
B. Base
C. Derived
D. 编译失败,全特化版本无法被继承
答案:A
解析:
Processor
匹配偏特化(指针类型),输出 Pointer Processor
。Processor
匹配全特化,输出 Int Pointer Processor
。答案:B
解析:
print(10)
优先匹配非模板函数 void print(int)
。print(3.14)
匹配通用模板(T = double
)。答案:B
解析:
Checker
匹配通用模板(T = int
),输出 Non-const T
。Checker
匹配偏特化(const T
),输出 Const T
。答案:A
解析:
Counter
实例化通用模板:两次构造(a
, b
),count
累加为 2。Counter
全特化版本:两次构造(c
, d
),每次构造 count += 2
,总为 4。答案:C
解析:
Derived
继承自 Base
的全特化版本。Derived::print()
隐藏了基类的 print()
(非虚函数),因此直接调用派生类方法。这些题目覆盖了模板特化的核心机制,包括优先级规则、类型约束、静态成员和继承交互。通过分析这些场景,可以深入理解模板特化在静态多态中的应用,并避免常见陷阱(如函数模板与重载的优先级混淆)。