C++使用模板增强 lexical_cast ?
我正在尝试构建一个将程序设置存储为 std::map 的类。由于所有程序设置都存储为字符串,我想要一个访问器方法,可以将程序设置返回到相关类型。我是 C++ 模板新手,这是我的第一次尝试:
class Settings
{
public:
Settings(void);
virtual ~Settings(void);
enum SettingName {HomePageUrl, WindowWidth};
template<class T>
T Get(SettingName name)
{
return boost::lexical_cast<T>(settings_[name]);
}
template<class T>
void Set(SettingName name, T value)
{
settings_[name] = boost::lexical_cast<CString>(value);
}
private:
std::map<SettingName, CString> settings_;
};
但是,我遇到了编译器错误:
...boost\boost_1_46_1\boost\lexical_cast.hpp(776): error C2678: binary '>>' :
no operator found which takes a left-hand operand of type
'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
..settings.h(33) : see reference to function template instantiation
'Target boost::lexical_cast<CString,T>(const Source &)' being compiled
使用 boost 时,错误输出很长,我不太确定它出了什么问题。
I'm trying to build a class that stores program settings as a std::map. Since all the program settings are stored as strings I'd like an accessor method that can return the program setting casted to the relevant type. I'm new to templating in C++ and this is my first attempt:
class Settings
{
public:
Settings(void);
virtual ~Settings(void);
enum SettingName {HomePageUrl, WindowWidth};
template<class T>
T Get(SettingName name)
{
return boost::lexical_cast<T>(settings_[name]);
}
template<class T>
void Set(SettingName name, T value)
{
settings_[name] = boost::lexical_cast<CString>(value);
}
private:
std::map<SettingName, CString> settings_;
};
However, I'm getting a compiler errors:
...boost\boost_1_46_1\boost\lexical_cast.hpp(776): error C2678: binary '>>' :
no operator found which takes a left-hand operand of type
'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
..settings.h(33) : see reference to function template instantiation
'Target boost::lexical_cast<CString,T>(const Source &)' being compiled
With boost the error output is very long and I'm not really sure what's wrong with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CString 没有任何运算符<<
考虑使用 std::string
CString does not have any operator<<
Consider using std::string
lexical_cast 基本上尝试将对象写入流对象。
您需要定义 << 和 >> 运算符来写入您正在使用的类中的流才能正常工作。 (取决于你是在阅读还是在写作)
lexical_cast basically tries to write the object into a stream object.
you need << and >> operator defined to write to a stream in the class you're using for it to work. (depends if you're reading or writing)
如 文档 所示,boost::lexical_cast 确实它的转换基于几个事物的存在。源类型必须有一个运算符<<需要一个 std::ostream (或 std::wostream),并且目标类型必须有一个运算符>>它需要一个 std::istream (或 std::wistream)。这些函数的第一个参数是对流的非常量引用,第二个参数是对要发送/构造的类型的引用。
为了将设置名称转换为 T,该 T 必须具有运算符>>。需要一个输入流。同样,为了转换为CString,必须有一个运算符<<需要一个输出流。
As shown in the documentation, boost::lexical_cast does its conversion based on the presence of several things. The source type must have an operator<< that takes a std::ostream (or std::wostream), and the destination type must have an operator>> that takes a std::istream (or std::wistream). The first parameter to these function is a non-const reference to the stream, and the second parameter is a reference to the type to send/construct.
In order to convert a setting name to a T, that T must have an operator>> that takes an input stream. Similarly, in order to convert to a CString, there must be an operator<< that takes an output stream.