Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-03 16:19:48

Vicente BOTET wrote:
> De : "Stewart, Robert"
> > Vicente BOTET wrote:
> >
> > > In order to take care of types without default constructor
> > > I see several options:
> > >
> > > template
> > > Target convert_to(Source const& s, Target const& t);
> > >
> > > template
> > > Target& assign_to(Target& t, Source const& s);
> > >
> > > template
> > > struct default_value
> > > {
> > > T apply() { return T(); }
> > > };
> > >
> > > This meta-function will be used instead of declaring a
> > > defaulted variable in the string conversion function.
> > >
> > > T v(default_value::apply());
> >
> > None of those options work for types without a default
> > constructor *and* without a sentinel value.
> Why? For me the 3 options work.

Consider conversion to int, where all values are allowed:

int const fallback(/* what to use here? */);
int const t(convert_to<int>("some string", fallback));
if (t == fallback)
   // is this because the conversion failed or because
   // of the second parameter?
int value;
assign_to(value, "some string");
if (value == ???) // how test for failure?
Cannot specialize default_value because there is no suitable default value. Zero is not appropriate because it is within range.

In each case, because there is no special, sentinel value that is out of range, there is no way to detect conversion failure.

> > > As a user I would prefer the second one
> >
> > The name "assign_to" just seems odd to me. The problem is
> > that assigning "123" to an int is the wrong idea. It isn't
> > assignment but conversion. I understand you wanted a
> > different name to account for the different behavior, but
> > that's not it.
> assign_to is the functional for of operator= (assign). This
> operator can accept any type on its lhs, making a conversion. I
> think the name assign_to respect this schema.

I'm not arguing why you have the function, I'm just telling you that I don't find the name to be good.

I also don't thing assignment for conversions is appropriate.

> > > template
> > > Target convert_to(Source const& s, err_code& ec);
> >
> > That's fine if there are specific errors to report other than
> > "bad input." The err_code type should also support a safe-bool
> > conversion.
> I was thinking on the error code of the C++ standard.

Hmmm. Those codes aren't extensible. Do they really describe the sorts of errors likely to occur during such conversions? If the codes don't cover everything, then they won't work. You'd need a different list, though a similar design would work.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP

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, gregod at, cpdaniel at, john at