为什么 C++ std::Exception::const 成员是什么?
我很好奇为什么 std::exception::what
成员函数是 const
?
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
};
I'm curious as to why the std::exception::what
member function is const
?
class exception
{
public:
exception() throw() { }
virtual ~exception() throw();
/** Returns a C-style character string describing the general cause
* of the current error. */
virtual const char* what() const throw();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用
what()
成员函数不应修改exception
对象的可观察状态。通常,异常是通过 const 引用捕获的。例如,
如果
what()
成员函数不是 const 限定的,则此模式将不起作用,因为您无法从 catch 块内调用它。如果您不想在调用
what()
之前生成要从what()
返回的字符串,则可以将该字符串具体化为可变成员变量。Calling the
what()
member function should not modify the observable state of theexception
object.Typically, exceptions are caught by const reference. For example,
If the
what()
member function was not const-qualified, this pattern wouldn't work because you wouldn't be able to call it from within the catch block.If you don't want to generate the string to be returned from
what()
until it is called, you can materialize the string into a mutable member variable.因为它不会改变 Exception 实例。
一般来说,所有不改变其类实例的函数都应声明为 const,以便可以在 const 限定的变量上调用它们。
Because it doesn't mutate the
Exception
instance.In general, all functions that do not mutate their class instance should be declared
const
, so that they can be called onconst
-qualified variables.翻转问题。为什么它不是 const 呢?
如果它是“char *what() const”,您可以修改 What() 返回指针的内部 char 数组。如果异常()允许任意代码以这种方式操作其内部缓冲区,那将是极其愚蠢的,所以what()返回一个const char *,而不是一个char *。
如果它是“const char *what()”,没有const限定符,则表明调用what()将修改异常的内部状态。但事实并非如此,你也不会期望它会这么做。
所以我们有了“const char *what() const”。返回指向 const 数组的指针的 const 函数。结果是您可以在 const 引用上调用它。一般都有哪些例外情况。
毕竟,您通常不会更改异常,而是构造它们、抛出它们,然后让处理代码操纵它们而不更改它们。所以他们的成员函数应该是const的。
Flip the question. Why would it not be const?
If it was "char *what() const", you could modify the internal char array that what() is returning a pointer to. It'd be astoundingly stupid for exception() to allow arbitrary code to manipulate its internal buffers in that way, so what() returns a const char *, instead of a char *.
And if it was "const char *what()", without the const qualifier, it would indicate that calling what() would modify the internal state of the exception. Which it doesn't, and which you'd not expect it to do.
So we have what we have, "const char *what() const". A const function returning a pointer to a const array. And the result is that you can call it on a const reference. Which exceptions generally are.
After all, you don't usually change exceptions, you construct them, throw them, and then have the handling code manipulate them without changing them. So their member functions should be const.
原因可能是出于维护稳定的愿望。例如,如果进程内存不足,则可能会引发异常。如果是这种情况,修改“What”以使其需要更多空间将会适得其反。如果程序员在这种情况下尝试这样做,它将立即中止整个程序。可能不是你想要的。
同样,What 使用堆栈中的额外空间也是不合适的 - 因为这正是抛出异常期间 uwinding 的内容。
也就是说,boost 库有一个 boost::exception,确实允许您在异常向上传播时向其中添加材料。买者自负。
The reasoning probably came from a desire to uphold stability. For example, an exception could be thrown if a process runs out of memory. If that is the case, it would be counter productive to modify "What" so that it might need more space. If the programmer attempted that, under those circumstances, it would immediately abort the whole program. Probably not what you intended.
Likewise, it is not appropriate for the What to use extra space from the stack - since that is precisely what is uwinding during a thrown exception.
That said, the boost library has a boost::exception that does allow you to add material to it as the exception propogates upward. Caveat Emptor.