C ++ STL堆栈问题:为什么pop()不抛出异常,如果堆栈是空的? [英] C++ STL stack question: Why does pop() not throw an exception if the stack is empty?

查看:243
本文介绍了C ++ STL堆栈问题:为什么pop()不抛出异常,如果堆栈是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么std :: stack :: pop()不会抛出异常,如果堆栈是空的,没有什么可以弹出?

Why does std::stack::pop() not throw an exception if the stack is empty and there is nothing to pop?

一个专门的堆栈为我自己的代码,并想知道这种方法(需要一个手动检查是否堆栈是空的)与投掷异常的折衷。

(I'm designing a specialized Stack for my own code and would like to know the tradeoffs with this approach (which requires one to manually check if the stack is empty) vs. throwing an exception.

我的猜测这里将是,虽然C ++支持异常处理,它带有一个小的运行时开销,因此,为了最大的性能,决定不是在std :: stack :: pop)抛出异常。 p>

My guess here would be that although C++ supports exception-handling, it comes with a small runtime overhead, and therefore, for maximum performance, the decision was made not to throw an exception in std::stack::pop).

推荐答案

您是正确的。 C ++标准总是更好的性能安全。但是可能存在包括调试范围检查的STL实现。

You are correct. The C++ standard always prefers performance to safety. But there may be STL implementations that include debug range checks.

这篇关于C ++ STL堆栈问题:为什么pop()不抛出异常,如果堆栈是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆