在c++中,专门化被视为重载
specialization being seen as overloading in C++
我是模板化编程的初学者。
我在模板类中有三个模板函数:
// initialize the model (generic template for any other type)
template <typename ImgDataType>
void GrimsonGMMGen<ImgDataType>::InitModel(const cv::Mat& data) // data is an rgb image
{ ... }
template<>
void GrimsonGMMGen<cv::Vec3b>::InitModel(const cv::Mat& data)
{...}
template<>
void GrimsonGMMGen<float>::InitModel(const cv::Mat& data)
{ ... }
但是我得到一个错误说有重声明指向重声明in我记得以前使用过这样的专门化,效果很好。我哪里做错了?
我需要特殊化它们,因为我正在设置的一些数据结构需要我正在使用的图像类型的信息。
您在问题中尝试做的事情绝对有效:类模板的成员函数可以单独特化(完全)。例如:
#include <iostream>
template <typename T> struct Foo
{
void print();
};
template <typename T> void Foo<T>::print()
{
std::cout << "Generic Foo<T>::print()n";
}
template <> void Foo<int>::print()
{
std::cout << "Specialized Foo<int>::print()n";
}
int main()
{
Foo<char> x;
Foo<int> y;
x.print();
y.print();
}
现场演示
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 按参数C++类型专门化重载构造函数
- 在不丢失功能的情况下通过重载来专门化类
- 类模板专门化的操作符重载
- 在c++中,专门化被视为重载
- 如何在子类中重载模板化函数(专门化)
- 如何在不重载比较操作符的情况下为std::max专门化自定义类型
- 在std命名空间中重载(未专门化)模板
- 如何将模板化函数重载转换为部分专门化的模板化类静态方法