启用类为使用的boost :: lexical_cast的 [英] Enabling Classes for Use with boost::lexical_cast
问题描述
$ C从$ C片段 lexical_cast的:
类lexical_castable {
上市:
lexical_castable(){};
lexical_castable(常量的std ::字符串s):S_(S){}; 朋友的std :: ostream的运营商的LT;<
(性病:: ostream的和放O,常量lexical_castable&安培;乐);
朋友的std :: istream的运营商的GT;>
(的std :: istream的&安培;我,lexical_castable&安培;乐);私人的:
虚拟无效print_(的std :: ostream的和放大器; O)const的{
ØLT&;< S_<<\\ n;
} 虚拟无效read_(的std :: istream的&安培;我){常量
I>> S_;
} 标准::字符串S_;
};的std :: ostream的运营商的LT;≤(的std :: ostream的和放O,
常量lexical_castable&安培;勒){
le.print_(O);
返回O;
}的std :: istream的运营商的GT;>(的std :: istream的&安培;我,lexical_castable&安培;乐){
le.read_(ⅰ);
返回我;
}
根据文件,
模板< typename的目标,类型名称来源与GT;
目标lexical_cast的(常量源和放大器; ARG);
1>返回流ARG成一个标准库的结果
串基于流,然后作为一个目标对象
2>源是OutputStreamable
3>目标是InputStreamable
块引用>问题1 >对于用户定义类型(UDT),应OutputStreamable或InputStreamable总是要处理
的std ::字符串
?例如,给定包含一个简单的整数作为成员变量的类,当我们定义的运营商的LT;<
和运营商的GT;>
,什么是执行code样子?我必须整数转换为字符串?根据我的理解,似乎UDT总是要处理的std ::为了与
的boost ::工作的lexical_cast $ C字符串
$ C>和的boost :: lexcial_cast
所需要的中间的std ::字符串
做真正的转换作业。问题2 >为什么返回值
运营商的LT;<
或运营商的GT;>
上面code未参照的std :: ostream的&安培;
或STD istream的放大器::&;分别
?解决方案要使类可用以
的lexical_cast
,只定义了流运营它。
从<一个href=\"http://www.boost.org/doc/libs/1_48_0/doc/html/boost_lexical_cast/synopsis.html\">Boost.LexicalCast简介:
- 来源是OutputStreamable,这意味着一个
运营商的LT;&LT;
的定义,它接受一个STD: :ostream的
或的std :: wostream
在左侧对象和参数实例键入正确的。
- 目标是InputStreamable,这意味着一个
运营商的GT;&GT;
的定义,它接受一个STD: :istream的
或的std :: wistream
在左侧对象和结果的实例键入正确的。
- 目标是复制构造[20.1.3]。
- 目标是可缺省,这意味着它是可能的缺省初始化该类型[8.5,20.1.4]的一个目的。
块引用>
//内联的朋友,超出类的朋友,或者只是正常的自由函数
//取决于它是否需要访问个内构件
//或者也可以通过公共接口应付
//(仅使用一个版本)
MyClass类{
INT _i;
上市:
//内嵌版本
朋友的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器; OS,常量MyClass的放大器及; MS){
返回OS&LT;&LT; ms._i;
} //或超出friend类(内部类朋友的声明只)
朋友的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器; OS,常量MyClass的放大器及;毫秒); //为免费版功能
INT get_i()const的{返回_i; }
};//外的类继续
的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器; OS,常量MyClass的放大器及; MS){
返回OS&LT;&LT; ms._i;
}//免费功能,非好友
的std :: ostream的&放大器;运营商的LT;≤(的std :: ostream的和放大器; OS,常量MyClass的放大器及; MS){
返回OS&LT;&LT; ms.get_i();
}相同的课程,为
运营商的GT;方式&gt;
Code snippet from 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; }
Based on document,
template<typename Target, typename Source> Target lexical_cast(const Source& arg);
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
std::string
? For example, given a class containing a simple integer as member variable, when we define theoperator<<
andoperator>>
, 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 withstd::string
in order to work withboost::lexical_cast
andboost::lexcial_cast
needs the intermediatestd::string
to do the real conversion jobs.Question2> Why the return value of
operator<<
oroperator>>
in above code is not reference tostd::ostream&
orstd::istream&
respectively?解决方案To make your class usable with
lexical_cast
, just define the "stream" operators for it. From Boost.LexicalCast Synopsis:
- Source is OutputStreamable, meaning that an
operator<<
is defined that takes astd::ostream
orstd::wostream
object on the left hand side and an instance of the argument type on the right.- Target is InputStreamable, meaning that an
operator>>
is defined that takes astd::istream
orstd::wistream
object on the left hand side and an instance of the result type on the right.- Target is CopyConstructible [20.1.3].
- Target is DefaultConstructible, meaning that it is possible to default-initialize an object of that type [8.5, 20.1.4].
:
// 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(); }
The same of course for
operator>>
.这篇关于启用类为使用的boost :: lexical_cast的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!