在C ++ 11中抛出时,异常是否使用移动语义? [英] Does an exception use move semantics when thrown in C++11?

查看:103
本文介绍了在C ++ 11中抛出时,异常是否使用移动语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.drdobbs.com / cpp / practical-c-error-handling-in-hybrid-env / 197003350?pgno = 4

在本文中Herb Sutter解释说, exception需要一个异常的副本,因为它被创建为一个临时的,因此使用 std :: auto_ptr 来绕过复制开销。鉴于move语义在C ++ 11中可用,这还是必要的吗?

In this article Herb Sutter explains that throwing an exception requires a copy of the exception as it's created as a temporary and therefore uses an std::auto_ptr to get round the copy overhead. In light of move semantics being made available in C++11 is this still necessary?

推荐答案

标准允许


  • 省略将throw表达式的操作数指定的对象复制或移动到异常对象

  • 如果不改变程序的含义,则忽略将异常对象的副本或移动到与异常对象相同类型的catch子句变量中(即,如果您将重新抛出并且随后的捕获将突然

由于这些遗漏是允许的,规范需要 em>首先将副本或移动的来源视为右值。因此,这意味着如果可能,将移动相应的对象。当然,复制和移动精确仍然是首选。

Since these omissions are allowed, the spec requires to first regard the source of the copy or move as an rvalue. So this means that the respective objects will be moved if possible. Of course copy and move elision are still allowed as the first choice.

我被告知,catch子句参数作为一个右值初始化器的异常对象初始化器的考虑可能会从标准中删除(因为一般来说,不可能检测到所有情况下的行为当省略复制/移动时程序不变),因此我建议依赖此(上面的第二个项目符号)。

I was notified that the consideration of the exception object initializer of a catch clause parameter as an rvalue initializer will probably be dropped from the Standard (because in general it is not possible for all cases to detect when the behavior of the program is unchanged when omitting a copy/move), so I recommend to not rely on this (second bullet above).

您仍然可以依赖的是将局部变量移入异常对象,如 throw x ; (上面的第一个项目符号)。

What you can still rely about is the move of a local variable into the exception object, as in throw x; (first bullet above).

这篇关于在C ++ 11中抛出时,异常是否使用移动语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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