Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-05 13:32:11


Jeff Flinn wrote:
> Stewart, Robert wrote:
> > Vicente BOTET wrote:
> >> De : "Vladimir Batov"
> >>> "Stewart, Robert" wrote
>
> >>> Maybe to avoid the temptation of pairing up we could
> >>> simplify (at least from the user perspectives) down to:
> >>>
> >>> 1. T convert_cast(S)
> >>> 2. T convert_cast(S, T, throw_t =nothrow)
> >> I don't see why (2) the user could want to throw when is
> >>> giving a fallback. So the throw_t =nothrow is not needed.
> >
> > That's the non-DefaultConstructible, no-sentinel use case:
> >
> > struct example
> > {
> > example(int);
> > };
> >
> > If one wants an exception to indicate conversion failure,
> > an initial value is needed and the desire for an exception
> > must be noted:
> >
> > example const e(convert_cast<example>("1", 3, throw_));
>
> Sorry if I'm just being dense, but if the above throws, the
> default value is never used, so why supply it?

Think about how convert_cast() might be implemented:

template <class T, class S, formatting-stuff>
T
convert_cast(S _source, formatting-stuff _formatting)
{
   T result;
   std::stringstream s;
   s << _source;
   s >> _formatting >> result;
   if (!s)
   {
      throw something;
   }
   return result;
}

Since example cannot be default constructed, you'd need:

template <class T, class S, class U, formatting-stuff>
T
convert_cast(S _source, U value, formatting-stuff _formatting, throw_t)
{
   std::stringstream s;
   s << _source;
   s >> _formatting >> value;
   if (!s)
   {
      throw something;
   }
   return value;
}

> > Obviously, with your default_value CP, the second argument
> > isn't needed for the last use case:
> >
> > template <>
> > default_value<example>
> > {
> > static example
> > apply()
> > {
> > return example(3);
> > }
> > };
> >
> > example const e(convert_cast<example>("1")); // exception
>
> So it's really just the no-default function that needs to be
> told to throw or return an optional.

Yes.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
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