My reference is to a particular observation made in the following article:
Exception Handling: A False Sense of Security
Under the section Exceptions Thrown by T
the author makes the following observation:
The next operation on T we examine is the copy construction of the T object returned from pop:
template <class T>
T Stack<T>::pop()
{
if( top < 0 )
throw "pop on empty stack";
return v[top--]; // throw
}
...
The post-decrement of top appears in an argument expression to the copy constructor for T. Argument expressions are fully evaluated before their function is called. So top is decremented before the copy construction.
What is the notion of an argument expression for a function call as implied in this context? Is there a particular reference to the standard (or even cppreference) that would throw more light on this concept?
My reference is to a particular observation made in the following article:
Exception Handling: A False Sense of Security
Under the section Exceptions Thrown by T
the author makes the following observation:
The next operation on T we examine is the copy construction of the T object returned from pop:
template <class T>
T Stack<T>::pop()
{
if( top < 0 )
throw "pop on empty stack";
return v[top--]; // throw
}
...
The post-decrement of top appears in an argument expression to the copy constructor for T. Argument expressions are fully evaluated before their function is called. So top is decremented before the copy construction.
What is the notion of an argument expression for a function call as implied in this context? Is there a particular reference to the standard (or even cppreference) that would throw more light on this concept?
Share Improve this question edited Feb 7 at 5:43 CPlus 4,76844 gold badges30 silver badges73 bronze badges asked Feb 7 at 4:41 VinodVinod 1,08110 silver badges13 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2In C++20 draft N4860, you find the term "argument expression" at 6.9.1 [intro.execution] (11):
When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function.
(emphasis mine)
So, top--
is already executed, returning top
(before the decrement), then a copy of v[top]
is made, which might throw, leaving top
decremented.
C++23 draft N4950 has this slightly reworded in 6.9.1 [intro.execution] (11):
When invoking a function (whether or not the function is inline), every argument expression and the postfix expression designating the called function are sequenced before every expression or statement in the body of the called function.
(emphasis mine)
Also find it on https://eel.is/c++draft/intro.execution#11
v[top]
. So, there is a function call, but it is behind the scenes. I think that paragraph is very badly worded. – Tim Roberts Commented Feb 7 at 5:14top
will have already been decremented and the item in the stack will be lost. – Alan Birtles Commented Feb 7 at 7:27