为什么在STL容器中的交换成员函数不声明为noexcept? [英] Why are the swap member functions in STL containers not declared noexcept?

查看:199
本文介绍了为什么在STL容器中的交换成员函数不声明为noexcept?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3797 开始,C ++标准需要 swap 容器的函数不会抛出任何异常,除非另有规定 [container.requirements.general] (< §10)。

As of N3797 the C++ standard requires swap functions of containers to not throw any exceptions unless specified otherwise [container.requirements.general] (23.2.1§10).


  • 为什么 swap 成员函数指定不抛出未声明 noexcept

  • Why are the swap member functions that are specified to not throw not declared noexcept?

同样的问题适用于专业非会员 swap 重载。

The same question applies to the specialized non-member swap overloads.

推荐答案

,这里是一个帖子从丹尼尔·克鲁格勒在 std -disussion 邮件列表:


声明一个函数为无条件noexcept的内部策略是

The internal policy to declare a function as unconditional noexcept is explained in

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3279.pdf

使用所使用的术语在那篇文章中,std :: vector的交换函数
有一个缩小的契约,也就是说,它有关于
的参与对象的分配器的前提条件。这意味着,存在
呼叫者可能违反前提条件的可能性,并且应该允许
实现来表示我的不同的装置
比终止。因此,这些函数不应该是noexcept,
,但它应该有一个有效的元素Throws:Nothing,因为这个
适用于满足前提条件的情况。

With the terminology used in that paper, std::vector's swap function has a narrowing contract, that is it has preconditions in regard to the allocators of the participating objects. This means, there exists the possibility that callers may violate the preconditions and an implementation should be allowed to signal this my different means than by termination. Therefore such functions should not be noexcept, but it should have an effective element "Throws: Nothing" because this applies to the situation when the preconditions are satisfied.

链接

内部政策是对您问题的规范,正式回答。

Said internal policy is the canonical, official answer to your question.

这篇关于为什么在STL容器中的交换成员函数不声明为noexcept?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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