为什么匿名临时例外可以绑定到捕获站点的参考?
考虑
#include <iostream>
struct Foo{};
int main(){
try {
throw Foo();
} catch (Foo& e){
std::cout << "Caught";
}
}
输出 ,但是为什么呢?我以为catch
应该是const foo&amp;
。我忘记了什么?
Consider
#include <iostream>
struct Foo{};
int main(){
try {
throw Foo();
} catch (Foo& e){
std::cout << "Caught";
}
}
The output is Caught
, but why? I would have thought that the catch
should have been const Foo&
. What am I forgetting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不需要。
我怀疑您期望的是,将LVALUE引用到非const的情况下会存在问题。
请注意突出显示的“ LVALUE”。将非符合级别的引用绑定到LVALUE是没有问题的。
Sidenotes:
忽略了顶级CV预选赛,并且不需要在抛出的对象和处理程序类型之间匹配。
捕获参考可以修改异常对象。
It doesn't need to be.
I suspect that you are expecting that there would be a problem with binding of an lvalue reference to non-const.
Note the highlighted "lvalue". There's no problem binding a non-const lvalue reference to an lvalue.
Sidenotes:
Top level cv qualifiers are ignored and don't need to match between thrown object and handler type.
Catching a reference allows modifying the exception object.
这是因为在lvalue中考虑了抛出的对象。 [except.handle]/14 state:
This is because the thrown object is considered in lvalue. [except.handle]/14 states: