gpt4 book ai didi

c++ - 为什么 STL 容器中的交换成员函数没有声明为 noexcept?

转载 作者:IT老高 更新时间:2023-10-28 22:59:09 25 4
gpt4 key购买 nike

截至 N3797 C++ 标准要求容器的 swap 函数不抛出任何异常,除非另有说明[container.requirements.general](23.2.1§10 )。

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

同样的问题也适用于专门的非成员 swap 重载。

最佳答案

进一步到what refp said ,这是 Daniel Krügler 在 std-discussion 邮件列表上的帖子:

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

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.

( link )

上述内部政策是对您问题的规范、官方回答。

关于c++ - 为什么 STL 容器中的交换成员函数没有声明为 noexcept?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23754223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com