Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-06-06 07:31:45


From: "Mark Rodgers" <mark.rodgers_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, June 06, 2000 3:34 AM
Subject: Re: [boost] Plea for addition of stream_cast

> From: Ed Brey <brey_at_[hidden]>
>
> > What's benefit do you see in avoiding exceptions?
>
> I don't think exceptions should be used as a normal control
> mechanism; they should be reserved for exceptional circumstances.
> AFAIK, the costs of throwing an exception are quite high, and
> are not something we want to have to pay for regularly.

Do you plan to regularly stream_cast<> _invalid_ input in a critical inner
loop without stopping the loop? If not, using exceptions is probably an
efficiency win on a good compiler.

> > Returning default values on failure is ambiguous, unless you
> > know from your context that the default would be impossible.
>
> Agreed. But sometimes we may indeed know this:
>
> int n = stream_cast<int>(argv[1], 0);
> if (n < 5)
> {
> std::cerr << "x must be a number greater than 4.";
> return EXIT_FAILURE;
> }
>
> Do we care whether the user typed "fred" or "3"? Possibly not.

We surely don't care about the overhead of throwing an exception in this
case.

> Absolutely true. That's why I probably prefer the default value
> approach. As I understand it, the initial version returned a
> default value anyway, but with no way to specify what that default
> was. I think this can be improved by allowing the client to
> specify the default value, and using its presence as the way of
> suppressing exceptions. We can then add the throwing overload
> that some people want.

From everything I've seen, throwing an exception would be at least a good
default response, and probably a good response always. Can you give an
example where an exception would actually be a problem?

-Dave


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