1 // boost lexical_cast.hpp header -------------------------------------------//
3 // See http://www.boost.org for most recent version including documentation.
5 #ifndef BOOST_LEXICAL_CAST_INCLUDED
6 #define BOOST_LEXICAL_CAST_INCLUDED
8 // what: lexical_cast custom keyword cast
9 // who: contributed by Kevlin Henney, with alternative naming, behaviors
10 // and fixes contributed by Dave Abrahams, Daryle Walker and other
11 // Boosters on the list
12 // when: November 2000
13 // where: tested with MSVC 6.0, BCC 5.5, and g++ 2.91
15 #include <boost/config.hpp>
17 // Some sstream implementations are broken for the purposes of lexical cast.
18 # if defined(BOOST_NO_STRINGSTREAM)
19 # define BOOST_LEXICAL_CAST_USE_STRSTREAM
22 #ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM
32 // exception used to indicate runtime lexical_cast failure
33 class bad_lexical_cast : public std::bad_cast
36 // constructors, destructors, and assignment operator defaulted
38 // function inlined for brevity and consistency with rest of library
39 virtual const char * what() const throw()
41 return "bad lexical cast: "
42 "source type value could not be interpreted as target";
46 template<typename Target, typename Source>
47 Target lexical_cast(Source arg)
49 # ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM
50 std::strstream interpreter; // for out-of-the-box g++ 2.95.2
52 std::stringstream interpreter;
56 if(!(interpreter << arg) || !(interpreter >> result) ||
57 !(interpreter >> std::ws).eof())
58 throw bad_lexical_cast();
64 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
66 // Permission to use, copy, modify, and distribute this software for any
67 // purpose is hereby granted without fee, provided that this copyright and
68 // permissions notice appear in all copies and derivatives.
70 // This software is provided "as is" without express or implied warranty.
72 #ifdef BOOST_LEXICAL_CAST_USE_STRSTREAM
73 # undef BOOST_LEXICAL_CAST_USE_STRSTREAM