在类模板外部为指针定义函数
Define a function outside a class template for pointers
我想只为指针模板参数定义一个成员函数,并以另一种方式为其余参数类型定义一个成员函数。我尝试了以下代码:
#include <iostream>
template <class T, class W>
struct A
{
void foo();
};
template<class T, class W> void A<T*, W*>::foo(){ std::cout << "foo" << std::endl; }
int main(){ }
演示
目前尚不清楚为什么它不起作用。我按照在模板声明中指定的顺序放置指针模板参数。实际上N4296::14.5.1/3 [temp.class]
:
类模板名称后面的模板参数列表 成员定义应按照与 一个用于成员的模板参数列表。
那么为什么代码不起作用呢?我按相同的顺序放置参数。
您必须将
整个结构定义为部分专用化,您不能只挑选单个成员进行专用化
[温度.class规格]
1 [...]应在首次使用类模板专用化之前声明部分专业化,该类模板专用化将利用部分专业化作为发生此类使用的每个翻译单元的隐式或显式实例化的结果;无需诊断。
#include <iostream>
template <class T, class W>
struct A
{
void foo();
};
template<class T, class W>
struct A<T*, W*>
{
void foo();
};
template<class T, class W> void A<T*, W*>::foo() { std::cout << "foo" << std::endl; }
int main() {}
相关文章:
- 用户定义函数中的指针和输入
- 使用"Task"函数指针队列定义作业管理器
- 通过函数指针定义类范围之外的方法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 为什么不能将字符指针定义为数组?
- 块作用域中的函数指针定义
- 从较小类型的指针定义的范围构造向量
- 函数指针定义中是否允许使用参数名称
- 是对范围的指针定义明确的比较
- 无法将指针定义为用户定义的类,但我可以直接实例化
- C++ GoogleTest 使用夹具 - 无法访问函数指针定义
- 是指针定义行为映射中 nullptr 的默认值
- 是否应始终为具有原始指针成员的类中的深层复制指针定义复制构造函数
- 在类模板外部为指针定义函数
- 如何使用函数指针定义多集
- 使用结构作为指针定义变量
- 使用非常量指针定义对常量数据的指针的引用有什么问题?
- 是否有办法为类的指针定义括号操作符?
- 为智能指针定义类型是不好的做法吗?
- 为什么函数指针定义适用于任意数量的'&'号或星号"*"?