Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-04-10 05:19:19


From: "Jesse Jones" <jesjones_at_[hidden]>

> At 12:02 AM +0300 4/10/01, Peter Dimov wrote:
> >From: "Jesse Jones" <jesjones_at_[hidden]>
> >
> >> 6) Trying to invoke an empty function seems like a precondition
> >> violation to me. Asserting instead of throwing seems preferable.
> >
> >I agree with most of your points, however invoking an empty function must
> >throw for boost::function<T&> to be default constructable. (What would
you
> >return from op() if it didn't throw?)
>
> There's no need to return anything. The client failed to meet his
> obligations so the function can do anything it likes. In this case it
> will crash on most OSs. But that's OK. :-)

But you need to return something for the code to compile.

Returning *(T*)0 is not a good practice. :-)

I think that the exception-throwing version can be quite useful.

FWIW, my own implementation first returned a default-constructed
return_type, then I thought about changing it to a precondition violation,
but in the end I decided in favor of the exception-throwing semantics. Why
litter the code with

if(f) f();

?

--
Peter Dimov
Multi Media Ltd.

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