why std::stack has separate top() and pop()
2013-07-21 15:24
435 查看
SGI explanation: http://www.sgi.com/tech/stl/stack.html One might wonder why pop() returns void, instead of value_type. That is, why must one use top() and pop() to examine and remove the top element, instead of combining the two in a single member function? In fact, there is a good reason for this design. If pop() returned the top element, it would have to return by value rather than by reference: return by reference would create a dangling pointer. Return by value, however, is inefficient: it involves at least one redundant copy constructor call. Since it is impossible for pop() to return a value in such a way as to be both efficient and correct, it is more sensible for it to return no value at all and to require clients to use top() to inspect the value at the top of the stack.
std::stack < T > is a template. If pop() returned the top element, it would have to return by value rather than by reference as per the of above explanation. That means, at the caller side it must be copied in an another T type of object. That involves a copy constructor or copy assignment operator call. What if this type T is sophisticated enough and it throws an exception during copy construction or copy assignment? In that case, the rvalue, i.e. the stack top (returned by value) is simply lost and there is no other way to retrieve it from the stack as the stack's pop operation is successfully completed!
the 2nd point might not be the reason of the initial design as STL is introduced into C++ before exceptions; it's a good point to make though.
std::stack < T > is a template. If pop() returned the top element, it would have to return by value rather than by reference as per the of above explanation. That means, at the caller side it must be copied in an another T type of object. That involves a copy constructor or copy assignment operator call. What if this type T is sophisticated enough and it throws an exception during copy construction or copy assignment? In that case, the rvalue, i.e. the stack top (returned by value) is simply lost and there is no other way to retrieve it from the stack as the stack's pop operation is successfully completed!
the 2nd point might not be the reason of the initial design as STL is introduced into C++ before exceptions; it's a good point to make though.
相关文章推荐
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact
- 算法导论 chapter 10.1 Page233 Stack push and pop
- android开发(41) Fragment中使用POP_BACK_STACK_INCLUSIVE达到一次跳转到栈底。类似Activity的 采用FLAG_ACTIVITY_CLEAR_TOP
- 【15】Design a stack which has push pop min max
- Stack that Support Push, Pop, and GetMin in Constant Time
- android开发(41) Fragment中使用POP_BACK_STACK_INCLUSIVE达到一次跳转到栈底。类似Activity的 采用FLAG_ACTIVITY_CLEAR_TOP
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
- stack manipulation push and pop
- Framework Topics:Activities:Tasks and Back Stack
- Tasks and Back Stack
- learning - Haskell AND Lisp vs. Haskell OR Lisp - Programmers Stack Exchange
- The connection to adb is down, and a severe error has occured.
- CentOS:ECDSA host key "ip地址" for has changed and you have requested strict checking(转)
- 【Android】任务和返回栈(tasks and back stack)
- 3D Touch和peek and pop和force
- The connection to adb is down, and a severe error has occured
- The connection to adb is down, and a severe error has occured.
- android 运行时出现The connection to adb is down, and a severe error has occured.(转)
- stack and heap
- Its 2010 and your browser has an assembler