如何最好地处理从 const char * 到模板类型的转换
How do I best handle conversion into template type from const char *
我有一个模板函数,我需要在其中将一个 const char * 转换为我的模板值。
我知道这个 const char * 最初是从 ascii 文本文件中读取的。我当前的代码如下所示:
template <typename T>
bool Get(T &value, std::string const &query, T const &default)
{
const char* result = DataHandler.GetValue(query);
if (result != NULL)
{
value = static_cast<T>(result); //Here is the issue
return true;
}
value = default
return false;
}
将此与 int 结合使用时出现错误
error C2440: 'static_cast' : cannot convert from 'const char *' to 'int'
有没有办法可以将 char* 无缝转换为我的类型 T,我在 SO 上找不到这个问题的答案。
在最坏的情况下,我可以针对我期望的 10 种类型进行说明,如果不是其中一种,则会给出错误,但如果可能的话,我宁愿不这样做。
Is there a way I can convert the char* to my type T seamlessly,
没有
无法使从字符串到类型的转换自动适用于所有类型。必须为每个 class 实施此类转换。通常,它是通过为 std::istream
实现流提取运算符 >>
来完成的。内置类型如 int
和一些标准类型如 std::string
已经有这样的运算符。然后你可以这样做:
std::istringstream istream(result);
int i;
istream >> i;
在不知道你到底要什么的情况下,使用@eerorika 给出的答案,这里是进行转换的通用方法:
#include <sstream>
#include <string>
#include <iostream>
template <typename T>
struct DefaultConverter
{
static T Convert(const char *result)
{
std::istringstream istream(result);
T i;
istream >> i;
return i;
}
};
template <typename T, typename Converter = DefaultConverter<T>>
bool Get(T &value, std::string const &query, T const &defaultVal)
{
const char* result = "100";
value = Converter::Convert(result);
return true;
}
int main()
{
int test = 10;
Get(test, "abc123", test);
std::cout << test;
}
引用 DataHandler
函数的代码对于说明上面的功能并不重要。
基本上,Get
函数有一个额外的参数,即一个转换模板,它有一个可用的 Convert
函数,可以被 Get
调用。请注意,默认转换器仅使用@eerorika 给出的先前答案中所示的代码。
这使您可以灵活地提供自己的转换器,该转换器具有 Convert
功能,可以执行任何自定义操作。
例如:
struct SomeMPEGObject
{
SomeMPEGObject() {}
SomeMPEGObject(const char *) {}
};
struct MPEGConverter
{
static SomeMPEGObject Convert(const char *result)
{
return SomeMPEGObject(result);
}
};
//...
SomeMPEGObject mpeg;
Get<SomeMPEGObject, MPEGConverter>(mpeg, "12345", mpeg);
这将在不更改 Get
函数中的任何代码的情况下工作。
我有一个模板函数,我需要在其中将一个 const char * 转换为我的模板值。 我知道这个 const char * 最初是从 ascii 文本文件中读取的。我当前的代码如下所示:
template <typename T>
bool Get(T &value, std::string const &query, T const &default)
{
const char* result = DataHandler.GetValue(query);
if (result != NULL)
{
value = static_cast<T>(result); //Here is the issue
return true;
}
value = default
return false;
}
将此与 int 结合使用时出现错误
error C2440: 'static_cast' : cannot convert from 'const char *' to 'int'
有没有办法可以将 char* 无缝转换为我的类型 T,我在 SO 上找不到这个问题的答案。
在最坏的情况下,我可以针对我期望的 10 种类型进行说明,如果不是其中一种,则会给出错误,但如果可能的话,我宁愿不这样做。
Is there a way I can convert the char* to my type T seamlessly,
没有
无法使从字符串到类型的转换自动适用于所有类型。必须为每个 class 实施此类转换。通常,它是通过为 std::istream
实现流提取运算符 >>
来完成的。内置类型如 int
和一些标准类型如 std::string
已经有这样的运算符。然后你可以这样做:
std::istringstream istream(result);
int i;
istream >> i;
在不知道你到底要什么的情况下,使用@eerorika 给出的答案,这里是进行转换的通用方法:
#include <sstream>
#include <string>
#include <iostream>
template <typename T>
struct DefaultConverter
{
static T Convert(const char *result)
{
std::istringstream istream(result);
T i;
istream >> i;
return i;
}
};
template <typename T, typename Converter = DefaultConverter<T>>
bool Get(T &value, std::string const &query, T const &defaultVal)
{
const char* result = "100";
value = Converter::Convert(result);
return true;
}
int main()
{
int test = 10;
Get(test, "abc123", test);
std::cout << test;
}
引用 DataHandler
函数的代码对于说明上面的功能并不重要。
基本上,Get
函数有一个额外的参数,即一个转换模板,它有一个可用的 Convert
函数,可以被 Get
调用。请注意,默认转换器仅使用@eerorika 给出的先前答案中所示的代码。
这使您可以灵活地提供自己的转换器,该转换器具有 Convert
功能,可以执行任何自定义操作。
例如:
struct SomeMPEGObject
{
SomeMPEGObject() {}
SomeMPEGObject(const char *) {}
};
struct MPEGConverter
{
static SomeMPEGObject Convert(const char *result)
{
return SomeMPEGObject(result);
}
};
//...
SomeMPEGObject mpeg;
Get<SomeMPEGObject, MPEGConverter>(mpeg, "12345", mpeg);
这将在不更改 Get
函数中的任何代码的情况下工作。