Boost logo

Boost :

From: Kevlin Henney (kevlin_at_[hidden])
Date: 2001-02-19 08:32:56


In message <01021915114101.11060_at_chronos>, Vladimir Prus
<ghost_at_[hidden]> writes
>At the moment lexical_cast<string>(whatever) will fail if 'whatever' decides
>to insert spaces in its representation. I think that it is not very
>convenient. Is it reasonable to make casts to std::string special case,
>returning stringstream::str()?

You're right that it's not convenient, but I would hesitate to make
std::string a special case.

>I haven't found discussion of this issue either in documentation or in
>mailing list archive.

No, but John Maddock did point this one out to me a while ago.
Unfortunately I did not have the chance to fix it before it went into
CVS, so my fault.

>It seems like implementing specialized behaviour is
>quite simple (I actually done it), using auxillary functions:
>
>template<class Target>
>void lexical_cast_aux(Target&, std::stringstream&) {....}
>
>void lexical_cast_aux(std::string&, std::stringstream&) {....}

A more general solution would be to replace the expression

        ... || !(interpreter >> result) || ...

by

        ... || !interpret(interpreter, result) || ...

where interpret is defined as

        template<typename Target>
        bool interpret(std::stringstream &source, Target &result)
        {
                return source >> result;
        }

        template<typename Char, typename Traits, typename Allocator>
        bool interpret(
                std::stringstream &source,
                std::basic_string<Char, Traits, Allocator> &result)
        {
                result = source.str();
                source.setstate(std::ios_base::eofbit);
                return true;
        }

I haven't tested this, but I guess it should be something along these
lines. It's also open, allowing for user-based extension if we want to
expose this as a non-implementation detail at some future point.

Kevlin
____________________________________________________________

  Kevlin Henney phone: +44 117 942 2990
  Curbralan Limited mobile: +44 7801 073 508
  mailto:kevlin_at_[hidden] fax: +44 870 052 2289
  http://www.curbralan.com
____________________________________________________________


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