函数模板专业化和Abrahams/Dimov示例
Function template specialization and the Abrahams/Dimov example
(我假设知道这个问题中的Abrahams/Dimov示例。)
假设在一个类似这样的头中有一些第三方代码,您不能修改:
template<class T> void f(T); // (1) base template 1
template<class T> void f(T *); // (2) base template 2
template<> void f<>(int *); // (3) specialization of (2)
问题是:
如果我已经按原样得到了上面的声明,我现在是否有可能将基础模板1专门用于T = int *
(例如)的情况?
或者仅仅是对基础模板2的声明就意味着基础模板1不能再专门化了(至少对于指针来说)?
您可以通过在函数名称后的尖括号中显式指定模板参数来重载(1)(参见C++11 Standard 14.7.3)
#include <iostream>
using namespace std;
template<class T> void f(T) // (1) base template 1
{
cout << "template<class T> void f(T)" << endl;
}
template<class T> void f(T *) // (2) base template 2
{
cout << "template<class T> void f(T *)" << endl;
}
//template<> void f<>(int *); // (3) specialization of (2)
template<> void f<int*>(int *) // (4) specialization of (1)
{
cout << "f<int*>(int *)" << endl;
}
int main() {
int i;
f(&i); // calls (2) since only base-templates take part in overload resolution
return 0;
}
你可以试着然后来找我们。但我不明白为什么它不起作用。如果是T = int*
,它将按您的意愿工作。因此,no2将是int* *
的参数
相关文章:
- 激励'inline'说明符的真实世界示例?
- 为什么示例代码访问IUnknown中已删除的内存
- 以下示例中如何避免代码复制?C++/库达
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 示例C++项目编译中的警告
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 为什么理解这个递归示例如此难以转化为直觉?
- Eventloop 卡住:NAO C++ SDK OnFaceDetection 示例
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 适用于 WebView2 旧版本的示例应用程序
- 即使直接从官方示例中复制,也找不到未知类型名称QML_ELEMENT和 QML 模块
- 将数组/向量发送到 c++ 脚本的 cython 示例
- 了解提升执行器示例
- 请求最简单的 OpenMP 目标 GPU 示例
- 两种模板示例有什么区别?
- 有人可以在下面的示例 C++ 程序中解释字符数组 KCHAR1 的奇怪行为吗?
- Distirbute Candy - 查找问题的最小可重现示例
- 函数模板专业化和Abrahams/Dimov示例