对于像 std::bad_alloc
这样的标准化异常,exception::what()
返回的字符串在不同的编译器中是否应该相同?
例如,我使用过的几个编译器都会为 bad_alloc::what()
返回 bad allocation
。是否有某种原因没有返回更具描述性的字符串,例如 Memory allocation failure
?
最佳答案
Is the string returned by
exception::what()
expected to be the same across compilers?
不,这不是标准化的。
以你的bad_alloc
为例:
[C++11: 18.6.2.1/5]:
virtual const char* what() const noexcept;
Returns: An implementation-defined NTBS.
(NTBS 的意思是“空终止字节串”;它是您关心的“实现定义的”。)
充其量,这种模式可以在所有标准异常定义中找到。
Is there some reason a more descriptive string such as Memory allocation failure isn't returned?
可能是因为它更长,而且实际上没有更具描述性。像“Memory allocation failure”这样冗长的字符串是完全没有必要的;提供信息的短字符串总是可以在调用代码时扩展为更加人性化。
关于c++ - 来自 exception::what() 的异常描述是否标准化为标准异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20666988/