
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back. In my code I use the following workaround: template<class U, class T> inline typename boost::disable_if<boost::is_same<T, U>, U>::type lexical_cast(T const& t) { return boost::lexical_cast<U>(t); } template<class T> inline T const& lexical_cast(T const& t) { return t; } Does it make any sense to put it in lexical_cast.hpp? -- Maxim Yegorushkin

"Maxim Yegorushkin" <e-maxim@yandex.ru> wrote
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back. In my code I use the following workaround:
template<class U, class T> inline typename boost::disable_if<boost::is_same<T, U>, U>::type lexical_cast(T const& t) { return boost::lexical_cast<U>(t); }
template<class T> inline T const& lexical_cast(T const& t) { return t; }
Does it make any sense to put it in lexical_cast.hpp?
Unless somebody already started using it to get the first token from a string, like: boost::lexical_cast<string>(string("I am a string with spaces")); // now returns just "I" OTOH, the code like this probably deserves to be broken :) Arkadiy

From: "Arkadiy Vertleyb" <vertleyb@hotmail.com>
"Maxim Yegorushkin" <e-maxim@yandex.ru> wrote
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back. In my code I use the following workaround:
template<class U, class T> inline typename boost::disable_if<boost::is_same<T, U>, U>::type lexical_cast(T const& t) { return boost::lexical_cast<U>(t); }
template<class T> inline T const& lexical_cast(T const& t) { return t; }
Does it make any sense to put it in lexical_cast.hpp?
Unless somebody already started using it to get the first token from a string, like:
boost::lexical_cast<string>(string("I am a string with spaces")); // now returns just "I"
lexical_cast doesn't work like that, anymore. Now, it handles strings with whitespace in them, so the above would have identical semantics to Maxim's suggestion. I've mailed Kevlin Henney about this thread (as I don't think he follows the Boost list). Regards, Terje

"Arkadiy Vertleyb" <vertleyb@hotmail.com> writes:
"Maxim Yegorushkin" <e-maxim@yandex.ru> wrote
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back. In my code I use the following workaround:
template<class U, class T> inline typename boost::disable_if<boost::is_same<T, U>, U>::type lexical_cast(T const& t) { return boost::lexical_cast<U>(t); }
template<class T> inline T const& lexical_cast(T const& t) { return t; }
Does it make any sense to put it in lexical_cast.hpp?
Unless somebody already started using it to get the first token from a string, like:
boost::lexical_cast<string>(string("I am a string with spaces")); // now returns just "I"
I can think of other uses. You might lexical_cast T->T in order to normalize the value somehow. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
I can think of other uses. You might lexical_cast T->T in order to normalize the value somehow.
Normalize? Hmm. How do you normalize int or char? ;-) I can think of UDT that has some non-serialized members and those members you could reset. lexical_cast is not a best tool for this, IMO. -- Alexander Nasonov

Alexander Nasonov <alnsn@yandex.ru> writes:
David Abrahams wrote:
I can think of other uses. You might lexical_cast T->T in order to normalize the value somehow.
Normalize? Hmm. How do you normalize int or char? ;-)
You're just joking, right? You wouldn't do this for simple builtins.
I can think of UDT that has some non-serialized members and those members you could reset. lexical_cast is not a best tool for this, IMO.
Maybe, maybe not. Sometimes convenience is more important than doing it the best way. It's almost always better to provide uniformity than to have special cases that break because they *might* just be an error. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams <dave@boost-consulting.com> wrote:
Alexander Nasonov <alnsn@yandex.ru> writes:
David Abrahams wrote:
I can think of other uses. You might lexical_cast T->T in order to normalize the value somehow.
[]
I can think of UDT that has some non-serialized members and those members you could reset. lexical_cast is not a best tool for this, IMO.
Maybe, maybe not. Sometimes convenience is more important than doing it the best way. It's almost always better to provide uniformity than to have special cases that break because they *might* just be an error.
I would like a cast to do cast only, not normalize. For normalize I would use something named appropriately, so that a reader of my code wouldn't be obfuscated. -- Maxim Yegorushkin

On 9/28/04 7:54 AM, "David Abrahams" <dave@boost-consulting.com> wrote:
Alexander Nasonov <alnsn@yandex.ru> writes:
David Abrahams wrote:
I can think of other uses. You might lexical_cast T -> T in order to normalize the value somehow.
Normalize? Hmm. How do you normalize int or char? ;-)
You're just joking, right? You wouldn't do this for simple builtins. [TRUNCATE]
Maybe you would for the floating-point built-in types?... -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com

Arkadiy Vertleyb <vertleyb@hotmail.com> wrote: []
boost::lexical_cast<string>(string("I am a string with spaces")); // now returns just "I"
OTOH, the code like this probably deserves to be broken :)
I would agree, if you used type 'word' rather than 'string'. -- Maxim Yegorushkin

Maxim Yegorushkin wrote:
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back.
Do you mean you sometimes use lexical_cast to cast between two types, neither of which is std::string? Could you tell why it's usefull -- it's very interesting since I never needed that myself. Wouldn't some more direct cast be more appropriate in that case? - Volodya

Vladimir Prus <ghost@cs.msu.su> wrote:
Maxim Yegorushkin wrote:
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back.
Do you mean you sometimes use lexical_cast to cast between two types, neither of which is std::string?
Rather both of them are. Do you still need an example? -- Maxim Yegorushkin

Maxim Yegorushkin wrote:
Vladimir Prus <ghost@cs.msu.su> wrote:
Maxim Yegorushkin wrote:
In generic code lexical_cast<> is sometimes used as a cast to the same type. I would like to shortcut this rather then going all the way by writing it to a stream and reading back.
Do you mean you sometimes use lexical_cast to cast between two types, neither of which is std::string?
Rather both of them are. Do you still need an example?
No. But in that case, I'm not sure it's good to use disable_if. According to tests, lexical_cast is much more portable than enable_if/disable_if and you don't need the generic lexical_cast<T>(T) case, only the string case. I really wonder if somebody ever used lexical cast to convert between two non-string types? - Volodya

Vladimir Prus <ghost@cs.msu.su> wrote:
But in that case, I'm not sure it's good to use disable_if. According to tests, lexical_cast is much more portable than enable_if/disable_if and you don't need the generic lexical_cast<T>(T) case, only the string case.
I see two solutions here: portable #ifdef's or add another version of lexical_cast with a different name. BTW, in my projects I use const_string<>, which lexical_cast<> has never heard of. -- Maxim Yegorushkin
participants (7)
-
Alexander Nasonov
-
Arkadiy Vertleyb
-
Daryle Walker
-
David Abrahams
-
Maxim Yegorushkin
-
Terje Slettebø
-
Vladimir Prus