From Jonathan de Boyne Polla · 1996-04-02 06:59 · #73529 in EMULATE
EB>
> void push(T *data);
> T *push(T *data);
>
> My decision was to use the latter for the reasons that I
> couldn't think of a good reason NOT to, and it made at
> least one thing easier. What would you have decided and
> why?
EB>
The only discussion that I've ever had on this subject was with the
`pop', not the `push', methods on a stack. I've never really given it
very much consideration myself.
It transpired that `void pop()' is safer than `T * pop()', because the
former is (claimed to be) more exception-safe. The argument goes that
whereas you can never be sure whether the object returned to you was
actually popped by `T * pop()' or whether an exception was raised, you
have a safer situation with `void pop()', since it requires that you
have a separate `T * top() const' function to actually obtain the top
element of the stack, which does not risk throwing exceptions and thus
returning a "wrong" value.
Personally, I don't agree with that argument. To my mind, both `void
pop()' and `T * pop()' are equally safe when it comes to exceptions. As
far as I can see, if `T * pop()' throws an exception, then control is
passed to the nearest enclosing catch clause, without evaluating the
remainder of the expression that called `T * pop()'. So the validity of
the (potential) return value is irrelevant. The code that takes the
value from the function and does something with it is never executed.
So since I've not found any significant or worrysome difference in the
semantics (so far), I tend to opt for the versions that allow me to
chain function calls, in this case `T * pushd (T *)' and `T * pop()'.
> JdeBP <___
X MegaMail 2.10 #0:
--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-725167) (2:440/4)