启用类与boost :: lexical_cast一起使用 [英] Enabling Classes for Use with boost::lexical_cast
问题描述
来自 lexical_cast 的代码段:
class lexical_castable {
public:
lexical_castable(){};
lexical_castable(const std :: string s):s_(s){};
friend std :: ostream operator<<
(std :: ostream& o,const lexical_castable& le);
friend std :: istream operator>>
(std :: istream& i,lexical_castable& le);
private:
virtual void print_(std :: ostream& o)const {
o< s_<<\ n;
}
virtual void read_(std :: istream& i)const {
i>> s_;
}
std :: string s_;
};
std :: ostream operator<<(std :: ostream& o,
const lexical_castable& le){
le.print_(o);
return o;
}
std :: istream operator>>(std :: istream& i,lexical_castable& le){
le.read_(i);
return i;
}
基于文档,
模板< typename目标,类型名源>
Target lexical_cast(const Source& arg);
1>将流arg的结果返回到标准库
基于字符串的流,然后作为Target对象。
2>源是OutputStreamable
3 > Target is InputStreamable
问题1 >对于用户定义类型(UDT),OutputStreamable或InputStreamable必须处理 Question2 >为什么 要使您的类可以使用 : 同样当然 Code snippet from lexical_cast: Based on document,
1> Returns the result of streaming arg into a standard library
string-based stream and then out as a Target object. 2> Source is OutputStreamable 3> Target is InputStreamable Question1> For User Defined Type (UDT), should the OutputStreamable or InputStreamable always have to deal with Question2> Why the return value of To make your class usable with : The same of course for 这篇关于启用类与boost :: lexical_cast一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! std :: string
?例如,当我们定义运算符<<
和运算符>> $ c时,给定一个包含简单整数作为成员变量的类$ c>,什么是实现代码看起来像?我必须将整数转换为字符串吗?基于我的理解,似乎UDT总是处理
std :: string
以便使用 boost :: lexical_cast
和 boost :: lexcial_cast
需要中间 std :: string
才能执行真正的转换作业。 p>
运算符<<
或 或
std :: istream& / code>
lexical_cast
,只需为其定义流运算符。
从 Boost.LexicalCast剧情介绍:
运算符<<
定义为需要 std :: ostream
或 std :: wostream
对象和右侧的参数类型的实例。
运算符>>
std :: istream
或 std :: wistream
右侧的结果类型的实例。
//内联的朋友,异常的朋友,或只是正常的自由函数
//取决于它是否需要访问internel成员
//或可以应付public interface
//(只使用一个版本)
class MyClass {
int _i;
public:
// inline version
friend std :: ostream& operator<<<(std :: ostream& os,MyClass const& ms){
return os< ms._i;
}
//或者类外的朋友(仅在类中的朋友声明)
friend std :: ostream& operator<<(std :: ostream& os,MyClass const& ms);
//用于自由函数版本
int get_i()const {return _i; }
};
//继续执行
std :: ostream& operator<<<(std :: ostream& os,MyClass const& ms){
return os< ms._i;
}
//自由函数,非朋友
std :: ostream& operator<<<(std :: ostream& os,MyClass const& ms){
return os< ms.get_i();
}
operator> / code>。
class lexical_castable {
public:
lexical_castable() {};
lexical_castable(const std::string s) : s_(s) {};
friend std::ostream operator<<
(std::ostream& o, const lexical_castable& le);
friend std::istream operator>>
(std::istream& i, lexical_castable& le);
private:
virtual void print_(std::ostream& o) const {
o << s_ <<"\n";
}
virtual void read_(std::istream& i) const {
i >> s_;
}
std::string s_;
};
std::ostream operator<<(std::ostream& o,
const lexical_castable& le) {
le.print_(o);
return o;
}
std::istream operator>>(std::istream& i, lexical_castable& le) {
le.read_(i);
return i;
}
template<typename Target, typename Source>
Target lexical_cast(const Source& arg);
std::string
? For example, given a class containing a simple integer as member variable, when we define the operator<<
and operator>>
, what is the implementation code looks like? Do I have to convert the integer as a string? Based on my understanding, it seems that UDT always has to deal with std::string
in order to work with boost::lexical_cast
and boost::lexcial_cast
needs the intermediate std::string
to do the real conversion jobs.operator<<
or operator>>
in above code is not reference to std::ostream&
or std::istream&
respectively?lexical_cast
, just define the "stream" operators for it.
From Boost.LexicalCast Synopsis:
operator<<
is defined that takes a std::ostream
or std::wostream
object on the left hand side and an instance of the argument type on the right. operator>>
is defined that takes a std::istream
or std::wistream
object on the left hand side and an instance of the result type on the right. // either inline friend, out-of-class friend, or just normal free function
// depending on whether it needs to access internel members
// or can cope with the public interface
// (use only one version)
class MyClass{
int _i;
public:
// inline version
friend std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms._i;
}
// or out-of-class friend (friend declaration inside class only)
friend std::ostream& operator<<(std::ostream& os, MyClass const& ms);
// for the free function version
int get_i() const{ return _i; }
};
// out-of-class continued
std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms._i;
}
// free function, non-friend
std::ostream& operator<<(std::ostream& os, MyClass const& ms){
return os << ms.get_i();
}
operator>>
.