Boost logo

Boost :

From: Early Ehlinger (earlye_at_[hidden])
Date: 2002-12-27 19:34:50


On the boost site for lexical_cast, a future direction is listed:

"Optimize the use of a stream away for identity conversions."

I recently encountered a situation where this optimization is not only nice
from a performance perspective, but is also necessary from a functional
perspective.

Deep inside of some nested templates, I had some code that effectively did
this due to the way the template parameters happened to play out in the
client code:

std::string source = "Hello there";
std::string dest = boost::lexical_cast< std::string >( source );

Normally, this would be a silly thing to do, but there are valid reasons
that the template code in place ended up doing this. (Please take my word
for it. It would likely take a month to go through the details of how I got
there...) Anyway, the surprising result of this was that the space in
"Hello there" caused the interpreter.eof() check in lexical_cast to fail.
So if that check were not present, dest would have been assigned the value
"Hello". Fortunately, the check was there, and instead I got a
bad_lexical_cast exception.

Is this the way lexical_cast is intended to work? I don't know, but it
certainly isn't what I found to be behavior that "feels right". So I
whipped up a modification to lexical cast that uses a template class and
overloaded static member functions to optimize calls like this. Basically,
I've got the current lexical_cast code inside of
boost::detail::lexical_caster<>::do_it<>, and provide a form of
boost::detail::lexical_caster<>::do_it that is not a template and accepts
the same type as the target of the cast.

lexical_cast returns the result of boost::detail::lexical_caster< Target
>::do_it( object ). If object is of the type Target, then the non-template
overload of ...::do_it is called, otherwise, the behavior is identical to
the current behavior.

I don't know if the community wants the new behavior or would prefer to keep
the old (and in my humble opinion, surprising) behavior. In either case, I
thought I'd share my efforts. Any feedback is greatly appreciated.

--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697 --
- RenderFarm - Lightwave , 3dSMax , Bryce , Maya , AfterEffects -
--- www.respower.com -- 200+ GHz Starting At USD$0.50/GHz*Hour --
----------------- SuperComputing For the Masses! ----------------
"I'm so busy programming my computer to let me be lazy, I never
get any rest!" - John Kaster
begin 666 lexical_cast.hpp
M+R\@(&)O;W-T(&QE>&EC86Q?8V%S="YH<' @:&5A9&5R(" M+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+R\-"@T*+R\@(%-E
M92!H='1P.B\O=W=W+F)O;W-T+F]R9R!F;W(@;6]S="!R96-E;G0@=F5R<VEO
M;B!I;F-L=61I;F<@9&]C=6UE;G1A=&EO;BX-"@T*(VEF;F1E9B!"3T]35%],
M15A)0T%,7T-!4U1?24Y#3%5$140-"B-D969I;F4_at_0D]/4U1?3$5824-!3%]#
M05-47TE.0TQ51$5$#0H-"B\O('=H870Z("!L97AI8V%L7V-A<W0_at_8W5S=&]M
M(&ME>7=O<F0_at_8V%S= T*+R\@=VAO.B @(&-O;G1R:6)U=&5D(&)Y($ME=FQI
M;B!(96YN97DL('=I=&@@86QT97)N871I=F4@;F%M:6YG+"!B96AA=FEO<G,-
M"B\O(" @(" @("!A;F0_at_9FEX97,@8V]N=')I8G5T960_at_8GD@1&%V92!!8G)A
M:&%M<RP_at_1&%R>6QE(%=A;&ME<B!A;F0@;W1H97(-"B\O(" @(" @("!";V]S
M=&5R<R!O;B!T:&4@;&ES= T*+R\@=VAE;CH@($YO=F5M8F5R(#(P,# -"B\O
M('=H97)E.B!T97-T960@=VET:"!-4U9#(#8N,"P_at_0D-#(#4N-2P_at_86YD(&<K
M*R R+CDQ#0H-"B-I;F-L=61E(#QB;V]S="]C;VYF:6<N:'!P/@T*#0HO+R!3
M;VUE('-S=')E86T@:6UP;&5M96YT871I;VYS(&%R92!B<F]K96X_at_9F]R('1H
M92!P=7)P;W-E<R!O9B!L97AI8V%L(&-A<W0N#0HC(&EF(&1E9FEN960H0D]/
M4U1?3D]?4U1224Y'4U1214%-*0T*(R @9&5F:6YE($)/3U-47TQ%6$E#04Q?
M0T%35%]54T5?4U124U1214%-#0HC(&5N9&EF#0H-"B-I9F1E9B!"3T]35%],
M15A)0T%,7T-!4U1?55-%7U-44E-44D5!30T*(R!I;F-L=61E(#QS=')S=')E
M86T^#0HC96QS90T*(R!I;F-L=61E(#QS<W1R96%M/@T*(V5N9&EF#0H-"B-I
M;F-L=61E(#QT>7!E:6YF;SX-"@T*;F%M97-P86-E(&)O;W-T#0I[#0H@(" @
M+R\@97AC97!T:6]N('5S960@=&\@:6YD:6-A=&4@<G5N=&EM92!L97AI8V%L
M7V-A<W0_at_9F%I;'5R90T*(" @(&-L87-S(&)A9%]L97AI8V%L7V-A<W0_at_.B!P
M=6)L:6,@<W1D.CIB861?8V%S= T*(" @('L-"B @("!P=6)L:6,Z#0H@(" @
M(" @("\O(&-O;G-T<G5C=&]R<RP_at_9&5S=')U8W1O<G,L(&%N9"!A<W-I9VYM
M96YT(&]P97)A=&]R(&1E9F%U;'1E9 T*#0H@(" @(" @("\O(&9U;F-T:6]N
M(&EN;&EN960_at_9F]R(&)R979I='D_at_86YD(&-O;G-I<W1E;F-Y('=I=&@@<F5S
M="!O9B!L:6)R87)Y#0H@(" @(" @('9I<G1U86P_at_8V]N<W0_at_8VAA<B J('=H
M870H*2!C;VYS="!T:')O=R_at_I#0H@(" @(" @('L-"B @(" @(" @(" @(')E
M='5R;B B8F%D(&QE>&EC86P_at_8V%S=#H@(@T*(" @(" @(" @(" @(" @(" @
M(")S;W5R8V4@='EP92!V86QU92!C;W5L9"!N;W0_at_8F4@:6YT97)P<F5T960@
M87,@=&%R9V5T(CL-"B @(" @(" @?0T*(" @('T[#0H-"B @("!N86UE<W!A
M8V4_at_9&5T86EL#0H@(" @>PT*(" @(" @("!T96UP;&%T93P@='EP96YA;64@
M5&%R9V5T(#X-"B @(" @(" @8VQA<W,@;&5X:6-A;%]C87-T97(-"B @(" @
M(" @(" @('L-"B @(" @(" @(" @('!U8FQI8SH-"B @(" @(" @(" @(" @
M<W1A=&EC(%1A<F=E="!D;U]I="@@5&%R9V5T('-O=7)C92 I#0H@(" @(" @
M(" @(" @(" @>PT*(" @(" @(" @(" @(" @(" @("!R971U<FX@<V]U<F-E
M.PT*(" @(" @(" @(" @(" @('T-"@T*(" @(" @(" @(" @("!T96UP;&%T
M93P@='EP96YA;64_at_4V]U<F-E(#X-"B @(" @(" @(" @(" @<W1A=&EC(%1A
M<F=E="!D;U]I="@@4V]U<F-E(&%R9R I#0H@(" @(" @(" @(" @(" @>PT*
M(R!I9F1E9B!"3T]35%],15A)0T%,7T-!4U1?55-%7U-44E-44D5!30T*(" @
M(" @(" @(" @(" @(" @("!S=&0Z.G-T<G-T<F5A;2!I;G1E<G!R971E<CL@
M+R\@9F]R(&]U="UO9BUT:&4M8F]X(&<K*R R+CDU+C(-"B,@96QS90T*(" @
M(" @(" @(" @(" @(" @("!S=&0Z.G-T<FEN9W-T<F5A;2!I;G1E<G!R971E
M<CL-"B,@96YD:68-"B @(" @(" @(" @(" @(" @(" @5&%R9V5T(')E<W5L
M=#L-"@T*(" @(" @(" @(" @(" @(" @("!I9B_at_A*&EN=&5R<')E=&5R(#P\
M(&%R9RD@?'P@(2AI;G1E<G!R971E<B ^/B!R97-U;'0I('Q\#0H@(" @(" @
M(" @(" @(" @(" @(" @("$H:6YT97)P<F5T97(@/CX@<W1D.CIW<RDN96]F
M*"DI#0H@(" @(" @(" @(" @(" @(" @(" @=&AR;W<@8F%D7VQE>&EC86Q?
M8V%S="@I.PT*#0H@(" @(" @(" @(" @(" @(" @(')E='5R;B!R97-U;'0[
M#0H@(" @(" @(" @(" @(" @?0T*(" @(" @(" @(" @?3L-"B @("!]#0H-
M"B @("!T96UP;&%T93QT>7!E;F%M92!487)G970L('1Y<&5N86UE(%-O=7)C
M93X-"B @("!487)G970@;&5X:6-A;%]C87-T*%-O=7)C92!A<F<I#0H@(" @
M>PT*(" @(" @("!R971U<FX_at_9&5T86EL.CIL97AI8V%L7V-A<W1E<CQ487)G
M970^.CID;U]I="@@87)G("D[#0H@(" @?0T*?0T*#0HO+R!#;W!Y<FEG:'0@
M2V5V;&EN($AE;FYE>2P@,C P,"P@,C P,2P@,C P,BX_at_06QL(')I9VAT<R!R
M97-E<G9E9"X-"B\O#0HO+R!097)M:7-S:6]N('1O('5S92P_at_8V]P>2P@;6]D
M:69Y+"!A;F0_at_9&ES=')I8G5T92!T:&ES('-O9G1W87)E(&9O<B!A;GD-"B\O
M('!U<G!O<V4@:7,@:&5R96)Y(&=R86YT960@=VET:&]U="!F964L('!R;W9I
M9&5D('1H870@=&AI<R!C;W!Y<FEG:'0_at_86YD#0HO+R!P97)M:7-S:6]N<R!N
M;W1I8V4_at_87!P96%R(&EN(&%L;"!C;W!I97,@86YD(&1E<FEV871I=F5S+@T*
M+R\-"B\O(%1H:7,@<V]F='=A<F4@:7,@<')O=FED960@(F%S(&ES(B!W:71H
M;W5T(&5X<')E<W,@;W(@:6UP;&EE9"!W87)R86YT>2X-"B\O#0H-"B-I9F1E
M9B!"3T]35%],15A)0T%,7T-!4U1?55-%7U-44E-44D5!30T*(R!U;F1E9B!"
M3T]35%],15A)0T%,7T-!4U1?55-%7U-44E-44D5!30T*(V5N9&EF#0H-"B-E
*;F1I9_at_T*#0H-"@``
`
end

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk