[Q] extendig lexical cast with custom classes

I developed a class cmoney_t and would like to use it uniformly with all the other datatypes I am using One of the things I would like to use is boost::lexical_caststd::string (const cmoney_t& in) However, when I simply just try to specialize the tempalte function lexical_cast it tells me that I cannot define that function within my namepsace (I have my own namespace) so wanted to know if there is a way to extend lexical_cast to support my custom classes thanks in advance -- V S P toreason@fastmail.fm -- http://www.fastmail.fm - Send your email first class

AMDG V S P wrote:
I developed a class cmoney_t and would like to use it uniformly with all the other datatypes I am using
One of the things I would like to use is boost::lexical_caststd::string (const cmoney_t& in)
However, when I simply just try to specialize the tempalte function lexical_cast
it tells me that I cannot define that function within my namepsace (I have my own namespace)
so wanted to know if there is a way to extend lexical_cast to support my custom classes
Lexical cast uses the standard stream operators. If you define std::ostream& operator<<(std::ostream&, const cmoney_t&); then lexical_cast should work. In Christ, Steven Watanabe

On Sat, Mar 28, 2009 at 1:11 AM, V S P
so wanted to know if there is a way to extend lexical_cast to support my custom classes
Yes, there is. Make your class is serializable to the ostream. lexical_cast uses std streams (stringstream) for conversion. Just overload the operator << in your namespace and write out any data to the stream how you would like it to appear. Regards, Ovanes
participants (3)
-
Ovanes Markarian
-
Steven Watanabe
-
V S P