c++模板的部分专门化
Partial specialization in c++ template
我有一个模板类来打印vector中的元素。我有两个指针和引用版本。
// HEADER
class Util {
...
template <class T>
static void print(const std::vector<T>* vectorArray);
template <class T>
static void print(const std::vector<T>& vectorArray);
...
static void printByteStream(const std::vector<unsigned char>& input);
...
};
// BODY
template <class T>
void Util::print(const std::vector<T>* vectorArray)
{
for (auto i = vectorArray->begin(); i != vectorArray->end(); ++i)
{
std::cout << *i << ":";
}
std::cout << std::endl;
}
template <class T>
void Util::print(const std::vector<T>& vectorArray)
{
return Util::print(&vectorArray);
}
template void Util::print(const std::vector<int>* vectorArray);
template void Util::print(const std::vector<std::string>* vectorArray);
template void Util::print(const std::vector<int>& vectorArray);
template void Util::print(const std::vector<std::string>& vectorArray);
我也有字节流的打印代码。
void Util::printByteStream(const std::vector<unsigned char>& input)
{
for (auto val : input) printf("\x%.2x", val);
printf("n");
}
我想教c++编译器,当我用T == unsigned char调用print时,用部分特化调用printByteStream。
我在正文中添加了这段代码。
void Util::print(const std::vector<unsigned char>& vectorArray)
{
return Util::printByteStream(vectorArray);
}
编译时,c++编译器报错找不到匹配的代码。可能出了什么问题?
error: prototype for 'void Util::print(const std::vector<unsigned char>&)' does not match any in class 'Util'
void Util::print(const std::vector<unsigned char>& vectorArray)
我认为你需要添加一个空模板
template <>
void Util::print(const std::vector<unsigned char>& vectorArray)
{
return Util::printByteStream(vectorArray);
}
我得查一下。我这台电脑上没有VS
相关文章:
- 是否可以对零模板参数进行模板专门化
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 如何为指向复杂值的迭代器专门化算法?
- 专门化模板覆盖函数/避免对象切片
- 我能否根据其运算符()的签名专门化可变参数模板参数
- 如何使用模板化类专门化模板化函数?
- 线程 std::调用未知类型,无法专门化函数错误
- 输入两个不专门化大小的矩阵
- 如何在模板类中专门化赋值运算符?
- 如何专门化容器和枚举的模板
- Clang拒绝类模板的嵌套类仅通过专门化定义的代码是正确的吗
- 取消专门化C++模板参数
- SFINAE的变分变量模板专门化
- 如何在编译时专门化大型模板函数中的小部分
- 如何在c++运算符()中专门化调用模板
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 如何专门化字符串数组的模板?
- 如何专门化模板
- 如何避免为模板化迭代器的每个可能的实例化专门化iterator_traits?