From: mfdylan (dylan_at_[hidden])
Date: 2002-01-24 19:28:50


--- In boost_at_y..., larsbj_at_l... wrote:
> "rogeeff" <rogeeff_at_m...> writes:
>
> | What specialization you mean? You want be able to make it more
> | efficient.
>
> specialization when to_type and from_type (or both) is string.
>
> --
> Lgb

I assume the issue is one of spaces primarily. Given it's possible
that any stream insertion for a UDT might generate a string with
spaces then basically the specialisation is only for the from_type,
ie:

template <class Source>
std::string lexical_cast<std::string>(Source arg)
{
# ifndef BOOST_NO_STRINGSTREAM
  ostringstream interpreter;
  interpreter << arg;
  return interpreter.str();
#else
  ostrstream interpreter;
  interpreter << arg;
  std::string ret = interpreter.str();
  interpreter.rdbuf()->freeze(0);
                     // strstream::freeze(0) not supported everywhere
  return ret;
#endif
}

Unfortunately this doesn't work for MSVC, you can do it via a
secondary function, something like:

template <class T>
T lexical_extract(std::stringstream& s)
{
        T t; s >> t; return t;
}

template <>
std::string lexical_extract<std::string>(std::stringstream& s)
{
        return s.str();
}

I was going to suggest this modification to lexical_cast sometime
back but never got around to it. Wide-char specialisations should be
added too of course, I don't think it's necessary to specialise for
basic_string<...> but it may actually be less work (providing you can
manage all the necessary partial template specialisations).

Dylan