|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-04-12 09:32:25
From: "David Abrahams" <abrahams_at_[hidden]>
> From: "Peter Dimov" <pdimov_at_[hidden]>
>
> > I'd also add these as possible answers:
> >
> > "Yes, if this simplifies client code considerably"; (eliminated return
> code
> > checks)
>
> I think this is technically the same answer. If the caller can avoid
> checking return codes, then he's willing to have the stack unwind... at
> least to the next enclosing catch block, which, to be convenient, must
> generally begin some statements after the calling statement, if not in a
> different function. Still, I think it is clearer to say the above than to
> not say it; when I get a chance I'll make that addition.
All four are a way to express aspects of 'the' answer.
> > "Yes, if you have to abort a long chain of operations";
>
> Hmm...
> op1();
> op2();
> op3();
> if (!op4()) return aborted;
> op5();
> op6();
> return success;
>
> You must have had something else in mind...
Yes, the original routine might not check, or the op's may span multiple
scopes. The key word is 'abort' - no turning back.
> > for(; first != last; ++first)
> > {
> > if(!found(*first)) f(*first); else break;
> > }
> >
> > -- an exception may be appropriate, the 'not found' situation has O(1)
> > potential.
>
> I guess I don't understand how you would use an exception here. Do you
mean
> it would make sense to throw in the break case, when the found returns
true?
> Or maybe you mean that it makes sense to throw from f()?
An annoying typo. Should be if(found()). The idea is that the 'not found'
situation happens at most once, so a 'throw' instead of a 'break' may be
appropriate.
> > About using 'assert' in a library: there's also the ODR problem to worry
> > about.
>
> True, but AFAICT nearly all modern code has some preprocessor-dependent
> definitions somewhere in header files, making this problem a fact of life.
NDEBUG is slightly different. If you provide a library that uses boost (and
boost has assert's) you must provide debug and non-debug versions and the
user must link to the appropriate version.
I'm not against assert per se - I use it all the time in my code, where I
have control - but I've never found a situation where an 'assert' buried
inside a third-party library helps more than it hinders.
-- 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