最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Argument expression of function call - Stack Overflow

programmeradmin1浏览0评论

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
  • What is the content of those block quotes ? – Eugene Commented Feb 7 at 4:50
  • @Eugene the text from the original article that I have reproduced in my question – Vinod Commented Feb 7 at 5:03
  • 4 The return statement is going to make a copy of 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:14
  • 1 Presumably the point is that if the copy constructor throws then top will have already been decremented and the item in the stack will be lost. – Alan Birtles Commented Feb 7 at 7:27
  • 4 Side note, I do not agree with that article at all stating that "exception handling will cause more ills than it solves". In the huge code bases I work with it is the other way around, it disentangles "happy flow" from "exceptional flow" making the code much more readable (try writing code that has checking return statements everywhere). Like any tool in C++ you do indeed need to know how and when to use it (but it doesn't mean it is a bad tool). There are some excellent books by Herb Sutter out there on exception safe programming – Pepijn Kramer Commented Feb 7 at 8:36
 |  Show 1 more comment

1 Answer 1

Reset to default 2

In 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

发布评论

评论列表(0)

  1. 暂无评论