可以复制elision发生在catch语句中吗? [英] Can copy elision occur in catch statements?

查看:139
本文介绍了可以复制elision发生在catch语句中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个带有副作用的复制构造函数的异常类。

Consider an exception class with a copy constructor with side-effects.

编译器可以跳过这里调用复制构造函数:

Can a compiler skip calling the copy constructor here:

try {
    throw ugly_exception();
}
catch(ugly_exception) // ignoring the exception, so I'm not naming it
{ }

这样做:

try {
    something_that_throws_ugly_exception();
}
catch(ugly_exception) // ignoring the exception, so I'm not naming it
{ }

(是的,我知道这是非常丑陋,这是受到另一个问题< a>)

(yes, I know this is all very ugly, this was inspired by another question)

推荐答案

是的,它可以在投掷和捕捉期间省略。对于捕获,只有当catch子句中指定的类型与异常对象的类型相同(保存为cv-qualifications)时才可以省略。对于更正式和详细的描述,请参阅C ++ 11 12.8 / 31。

Yes, it can be elided both during throwing and catching. For catching it can be elided only when the type specified in the catch clause is the same (save for cv-qualifications) as the type of the exception object. For more formal and detailed description see C++11 12.8/31.


...这种复制/在以下情况下允许(可以合并以消除多个副本):

...This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

...


  • 在throw-expression中,当操作数是一个非易失性自动对象(除了函数或catch子句参数)的名称,其范围不会超出最内层包含try-block(如果有),可以通过将自动对象直接构造为异常对象来忽略从操作数到异常对象(15.1)的复制/移动操作

...


  • 当异常处理程序的异常声明声明一个相同类型的对象(除了cv-qualification)作为异常对象(15.1),可以通过将异常声明作为异常对象的别名来处理来省略复制/移动操作,如果程序的含义除了对异常声明声明的对象的构造函数和析构函数的执行之外。

这篇关于可以复制elision发生在catch语句中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆