模板类专用化以处理其自己的类型
Template class specialization to handle its own type
我一直在玩模板来感受它们,我想在它自己的类型上做一个类专业化。我在互联网上搜索了一段时间,但我没有发现提到这一点。
例如,如果我有一个class Array
:
template<class T>
class Array{
...
void print();
}
T=Array<unspecified type>
时是否可以专门化方法print()
?
template<class T>
void Array<Array<T>>::print(){
//do something diffrent for array of array
//this code wont work
}
我设法做到了
template<>
void Array<Array<int>>::print(){
//print in matrix format
//this code works
}
我不认为这个功能非常有用,但我仍然很好
AFAIK 您只能对整个班级执行专业化。一旦我需要这样的东西(实际上,我只需要两个typedef
是不同的),所以我创建了一个辅助类,它只包含必须专用的成员,并使主体类继承它。
有一个称为部分专用化的功能,您可以在其中应用类似的东西。但是,我不认为您可以部分专用于成员函数而不部分专用于整个类。
相关文章:
- 没有为自己的结构调用列表推回方法
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 在遍历处理程序的向量时注册和注销处理程序
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- C++从对象自己的类中删除对象
- 在 cpp 自己的版本中对字符串进行哈希处理
- 使 ShowHint 使用多个不同的 Rect 处理自定义控件,每个 Rect 都有自己的 Hint
- C++联系人处理程序与自己的动态模板向量类
- 在 C++ 中创建自己的错误处理机制
- 如何在自己的函数中最好地处理Mat中的不同数字格式
- 模板类专用化以处理其自己的类型
- C++处理我自己的Exception类
- 如何将TortoiseOverlays与我自己的处理程序一起使用
- 子小部件应该处理自己的事件吗?
- 只有 1 种方法的类,让它处理自己的互斥锁是否安全?
- 重载操作符来处理我自己的类作为std类是一个好做法吗?
- 我应该编写自己的数据结构来处理语义解析还是直接在编译器项目中使用STL
- #ifndef如何处理全局声明的变量和我自己的变量