Boost logo

Boost :

From: Jan Langer (jan_at_[hidden])
Date: 2002-04-02 18:17:01


On Tue, 2 Apr 2002, Tim Shead wrote:
>widget.set_text("Position: " +
>boost::lexical_cast<std::string>(get_current_position));
>
>... you will (correctly) get a bad_lexical_cast exception, because the
>extraction operator for the target type (std::string) only extracts
>characters up to the first bit of whitespace. Converting objects to
>their string representations seems to me to be a common enough scenario
>to warrant their own function, such as "string_cast":
>
>// Basically a slight modification of Kevlin Henney's lexical_cast ...
>
>template<typename SourceType>
>std::string string_cast(SourceType arg)
>{
> std::stringstream interpreter;
>
> if(!(interpreter << arg))
> throw bad_string_cast();
>
> return interpreter.str();
>}

another related question: why is there no special lexical_cast for
strings which behaves as described by tim?

-- 
jan langer ... jan_at_[hidden]
"pi ist genau drei"

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