开发者

How to make STL throw exceptions instead of assertions?

I have to write tests for some poorly written C++ code. The problem is that the code causes STL assertions/debug breaks i开发者_C百科n many situations (usually caused by dumb mistakes like access beyond bounds of a string/vector), which result in termination of the tests.

Is it possible to make the STL throw exceptions (which can be caught) instead of just crashing?

I have to keep my changes to the original code to absolute minimum.

Thanks for help


It definitely depends on the STL you are using. That said, it might simply not be possible.

The Standard defines a number of guarantees (especially the C++0x one) and whether a function throws or not is part of those guarantees. For example std::vector<T>::operator[] will not throw.

That being said, there could be a number of tricks (specific compilation flags for debugging, or changing the macro used in the STL, if there is one), but it will be highly specific.

Another solution could be to try to run the program in a virtualized environment. For example, on Linux, you could run it under Valgrind. It will report the access errors, but won't actually stop the program from executing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜