【发布时间】:2014-06-27 06:39:46
【问题描述】:
我正在尝试为一个函数编写模板特化,该函数返回数值数组的最大值(通用版本)或 c 字符串数组的最长 c 字符串(特化)。如果我不使用 const-ness,我的函数原型看起来像这样
template <typename T>
T maxn(T* my_Tptr, unsigned int n);
template <>
char* maxn <char*> (char** my_cstrings, unsigned int n);
我的代码编译了。
但是,如果我尝试使用 const-ness,我的函数原型看起来像这样,
template <typename T>
T maxn(const T* my_Tptr, unsigned int n);
template <>
char* maxn <char*> (const char** my_cstrings, unsigned int n);
我的代码没有编译,编译器 (gcc) 打印出这个错误:
错误:'char* maxn(const char**, unsigned int)' 的模板 ID 'maxn' 与任何模板声明都不匹配。
我哪里错了?
【问题讨论】:
-
我不介意,但我真的想知道为什么您需要在源代码中进行构造?
-
template <> char const* maxn <char const*> (char const* const*, unsigned int);或template <> char* maxn <char*> (char* const*, unsigned int);
标签: c++ templates constants template-specialization specialization