Re: [boost] [lexical cast] Performance improvements

Message du 05/04/11 18:18 De : "Antony Polukhin" A : boost@lists.boost.org Copie à : Objet : Re: [boost] [lexical cast] Performance improvements
From Gruenke, Matt
I`ve looked at Boost.Conversion library at sandbox/conversion/. The idea is interesting, but:
1)
char ch = '0';
std::cout << boost::lexical_cast(ch) << " " << boost::numeric_cast(ch);
This code will output "0 48"
Good point. Thanks for this simple example.
We can not make your library output both values... There are some tags, but in both cases it will be boost::dummy::type_tag (or may be I didn`t get the idea)
I have not tried that, but i guess that you could use different tags for different conversions. I'm not sure the following will compile (I will try soon) template struct lex { typedef T type; }; template struct num { typedef T type; }; template typename Target::type convert_to(Source const& v , boost::dummy::type_tag > const&) { return lexical_cast(v); } template typename Target::type convert_to(Source const& v , boost::dummy::type_tag > const&) { return numeric_cast(v); } Then you can use them as follows std::cout << boost::convert_to< lex >(ch) << " " << boost::convert_to< num >(ch); This code will output "0 48" This is a little bit artificial, but it could work in some contexts. Note that I'm not saying the library should be used in tis way.
2) For each conversation user will need to write convert_to function overload. It will be much better, if user was required to write only one conversion function (to some intermediate type), and the library will be able to convert to any other type from intermediate type.
There is already lexical_cast and Boost.StringConvert (to be reviewed soon) that use an intermediary type or mechanism. Boost.Conversion is intended to provide an interface for direct conversions which should be more efficient in most of the cases (as you have shown). If you know that two types are convertible using an intermediary type, you can use convert_to_via Target target = convert_to_via(source);
I use lexical_cast<>() a lot, so I`d better improve it first.
I guess any improvement to lexical_cast that preserves the semantics will be welcome by the Boost comunity. Best, Vicente
participants (1)
-
Vicente BOTET