Boost logo

Boost :

Subject: Re: [boost] [convert] are you mixing default_value and error_value?
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-07-09 09:41:43


Gottlob Frege wrote:
> On Thursday, July 9, 2009, Vicente Botet Escriba
> <vicente.botet_at_[hidden]> wrote:
> >
> > What does this does?
> >
> > direction d = convert<direction>::from(str);
>
> Not compile, I guess.

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;

> You need to either write a specialization of convert, or maybe
> convert_traits. You had to specialize << anyhow.

The traits idea is inappropriate because the value to return for
each invocation can change.

> 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.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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