c++ - 普通函数重载模板函数但普通不重载模板类?
问题描述
为什么允许与函数模板同名的普通函数?但是,不允许使用与类模板同名的普通类。
template<typename T>
class A {};
class A {}; //compilation fails when uncommented
template<typename T>
void func(); //No problem compiling
void func();
int main() {
}
解决方案
类不能重载,只能重载函数。如果要“重载”一个类,请使用模板专业化:
// The generic class
template<typename T>
class A {};
// Specialization for int
template<>
class A<int> {};
// Specialization for std::string
template<>
class A<std::string> {};
// ...
A<int> my_int_a; // Uses the A<int> specialization
A<float> my_float_a; // Uses the generic A<T>
推荐阅读
- ibm-cloud - 是否可以在离线存储设备上部署模型?
- r - R: How do I plot multiple density plots on one chart, with columns pulled from excel
- couchbase - Couchbase 指标 sortcount 始终为零 - N1QL + .NET SDK
- python - Running a Python Scheduler every Monday?
- javascript - Express,Nodejs - 如何从客户端检测暂停/慢速互联网连接?
- flutter - 基于 Stream 的 Flutter 响应式导航
- google-cloud-platform - 在 GCP 的 DLP 中从 API 提供的多个操作不起作用
- python - Python客户端和NodeJS服务器简单通信
- python - tkinter GUI 中的意外数据泄漏
- overriding - 在我的渲染层中,我所有的收藏都被最后一个损坏了。maya中的python编程