gpt4 book ai didi

c++ - STL 容器元素是否明确要求 (noexcept) 可破坏?

转载 作者:可可西里 更新时间:2023-11-01 17:50:28 26 4
gpt4 key购买 nike

C++11(和 C++14)STL 容器有 noexcept析构函数和 clear()成员函数。

这意味着元素应该有 noexcept析构函数,或者至少存储在容器中的实际元素在被销毁时不应该抛出任何异常——或者更准确地说,相应的 allocator_traits<Allocator>::destroy调用不应抛出。

这是否在标准的任何地方指定为要求(明确或由另一个明确要求暗示)?如果不是,为什么?

我知道is_nothrow_constructible需要 noexcept析构函数,但是 is_constructible单独没有,并且容器要求是根据概念而不是类型特征模板指定的。

最佳答案

17.6.4.8/1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.

17.6.4.8/2 In particular, the effects are undefined in the following cases:

...

  • if any replacement function or handler function or destructor operation exits via an exception, unless specifically allowed in the applicable Required behavior: paragraph.

关于c++ - STL 容器元素是否明确要求 (noexcept) 可破坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29303939/

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