接受字符*的模板化函数
Templated function accepting char*
我有一个模板化函数,我不知道如何编写类型 *unsigned const char** ?! 的规范
我为简单的类型(int,long ...(做了如下:
template <typename T>
void ConvertTypeToString(const T p_cszValue, std::string& p_szValue)
{
p_szValue = p_cszValue;
}
//Template Specialization for int
template <>
void ConvertTypeToString<int>(const int p_iValue, std::string& p_szValue)
{
GetFormattedString(p_iValue,p_szValue);
}
//Template Specialization for double
template <>
void ConvertTypeToString<double>(const double p_dValue, std::string& p_szValue)
{
GetFormattedString(p_dValue,p_szValue);
}
在这里,我卡住了,我无法弄清楚我应该写什么? 下面的代码无法编译。
//for unsigned char* const
template <>
void ConvertTypeToString<unsigned char*>(const unsigned char* p_ucValue, std::string& p_szValue)
{
p_szValue.push_back(p_ucValue);
}
那么要编写的正确代码是什么,以考虑 usigned char* 常量?
比克你
您将const
放在错误的位置,它应该是:
template <>
void ConvertTypeToString<unsigned char*>(unsigned char* const p_ucValue, std::string& p_szValue)
{
p_szValue.push_back(p_ucValue);
}
通常最好添加重载而不是模板专用化。这允许您传递任何参数,包括指向 const 的指针:
void ConvertTypeToString(const unsigned char* const p_ucValue, std::string& p_szValue) { p_szValue.push_back(p_ucValue); }
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 将字符随机转换为大写的函数
- 固有构造函数的字符和访问级别
- constexpr 函数获取常量字符*
- 将字符缓冲区强制转换为函数指针
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 将字符串数组传递给接受常量字符**的函数
- 在函数中返回无符号字符数组,但不返回指针
- 如何返回实际值(在我的例子中是无符号字符数组)而不是来自 C++ 函数的指针?
- 在函数 strcpy() 中访问字符数组时出现分段错误
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 如何在字符函数中选择某些字符?
- 使用排序函数 c++ 对字符数组进行排序
- 使用 memcpy 函数C++复制字符数组
- C++返回反向字符数组的函数
- 模板总和函数冻结字符
- 在函数内初始化无符号字符指针将返回空指针
- C++ 错误:与调用<类构造函数(字符*&、字符*&、字符*&、字符*&)不匹配