开发者

Structured Exceptions (SE) from standard library calls

I've got code that calls ::fgetpos, which results in a kernel exception that can't be caught (I have option /ehs in my VS 2008 project). But I can't help think that standard library routines should never throw these kinds of exceptions.

update: I've tried calling ::fgetpos(0, &foo), which is clearly wrong, and I indeed received a 开发者_如何学编程kernel-level exception. I'm baffled. Why would the C++ standard library not perform the most basic of argument checks (check for null-pointer) and raise a std::invalid_argument?

Do any standard library routines perform such basic checks, or do they all happily cause program termination?


Why would the C++ standard library not perform the most basic of argument checks (check for null-pointer)

Because you can do that yourself if you need the check. The philosophy of C++ is that you don't pay for what you don't need. If I'm a smart programmer and will never pass invalid arguments to the function, why should I have my program's performance potentially suffer with needless checks?

This is why std::vector, for example, provides both operator[] and at(), where the latter performs a bounds-check and the former doesn't. If you need the check, add it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜