Boost logo

Boost :

Subject: Re: [boost] [convert] are you mixing default_value and error_value?
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2009-07-16 00:19:15


On Thu, Jul 9, 2009 at 9:41 AM, Stewart, Robert<Robert.Stewart_at_[hidden]> wrote:
> Gottlob Frege wrote:
>
> Assuming direction is the exemplar Vladimir has been using all
> along, that won't compile because direction has no default
> constructor.  The underlying behavior is the same as that of
> lexical_cast:
>
>   direction result;
>   std::istringstream stream(str);
>   stream >> result;
>   return result;
>
> That requires a default constructor.
>
> This code:
>
>   direction d(convert<direction>::from(str, direction::up));
>
> avoids the need for a default constructor via code like the
> following:
>
>   direction result(direction::up);
>   std::istringstream stream(str);
>   stream >> result;
>   return result;
>
>
>> Maybe the default/error form:
>>    convert<direction>::from(str, down)
>> shouldn't compile either (ie don't use error return as init
>> value).
>
> There isn't a choice.  In order to extract a direction from an
> istringstream, there must be a direction instance.  If direction
> has no default constructor, then the result *must* be initialized
> from the error return value.  I don't know what you think is or
> could be happening, but I hope the code I've shown above
> clarifies the matter.
>

I'm sure you are right, and the real limitation comes down to how
streams and << work. I was also trying to imagine the no_throw case:

   direction result(default_value);
   try
   {
       stream >> result;
   }
   catch (...)
   {
       result = error_value;
   }

   return result;

Although I'm not even sure
   - if you can guarantee that "return result" doesn't throw (in some
stupid complicated copy constructor)
   - if that is the meaning of no_throw
   - if that is the meaning of "error" (ie stream >> result could
still have an 'error' but not have thrown, depending on how it works,
and meaning, etc)

   - and still direction needed to be initialized, because, basically,
it doesn't have a construct-from-stream:

     {
          return direction(stream);
     }
   nor do most types, thus we use >> instead, as is defacto.

Basically I was leaving the implementation open to my imagination, and
focusing more on how it looks from the outside.

> _____
> Rob Stewart

Tony


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