本文介绍了如何在赋值运算符中使用noexcept与复制和交换惯用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

移动赋值运算符通常应声明为noexcept(即在STL容器中存储类型)。但是复制和交换惯例允许在一个单独的代码段中定义复制和移动分配操作符。在这种情况下,如何处理noexcept说明符?复制结构可以抛出,但我怀疑它是否可以违反noexcept指示符。

The move assignment operator should often be declared noexcept (i.e. to store the type in STL containers). But the copy-and-swap idiom allows both copy- and move- assignment operators to be defined in a single piece of code. What to do with noexcept specifier in this case? The copy construction can throw, but I doubt whether it can violate the noexcept specifier.

// Is it correct considering that T copy constructor can throw?
T& operator=(T other) noexcept;


推荐答案

em>呼叫者的侧,它不是你的功能的一部分。因此,它不能由您的函数控制,因此,您不能在 noexcept 规范中包含此信息。

Since the copy is made on the caller's side of the call, it is not part of what your function does. It can therefore not be controlled by your function and consequently, you can not include this information in the noexcept specification.

你唯一可以做的是安全地玩,并且添加两个选项到你的 noexcept 规范。当然,这意味着你得到一些假阴性。

The only thing you could do is to play it safe and add both options to your noexcept specification. Of course, that means you are getting some false-negatives.

这篇关于如何在赋值运算符中使用noexcept与复制和交换惯用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 02:11