gpt4 book ai didi

c++ - 接受 char* 的模板化函数

转载 作者:行者123 更新时间:2023-11-27 22:40:39 25 4
gpt4 key购买 nike

我有一个模板化函数,但我不知道如何为 *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 应该编写什么样的正确代码?

谢谢

最佳答案

你把 const 放在了错误的地方,它应该是:

template <>
void ConvertTypeToString<unsigned char*>(unsigned char* const p_ucValue, std::string& p_szValue)
{
p_szValue.push_back(p_ucValue);
}

关于c++ - 接受 char* 的模板化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com