gpt4 book ai didi

c++ - 为什么 vector 被认为是 nothrow_move_constructible?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:28 27 4
gpt4 key购买 nike

vector 的移动构造函数的规范是(从标准中复制出来的):

vector(vector&&);

注意缺少 noexcept .但是 gcc 4.8 和 Clang 3.2 都报告了 std::is_nothrow_move_constructible<std::vector<int>>::value返回真(即 1):

#include<vector>
#include<iostream>

int main()
{
std::cout << std::is_nothrow_move_constructible<std::vector<int>>::value << '\n';
}

造成这种明显差异的原因是什么?

最佳答案

标准允许实现根据方法加强异常规范

17.6.5.12 Restrictions on exception handling [res.on.exception.handling]

4 Destructor operations defined in the C++ standard library shall not throw exceptions. Every destructor in the C++ standard library shall behave as if it had a non-throwing exception specification. Any other functions defined in the C++ standard library that do not have an exception-specification may throw implementation-defined exceptions unless otherwise specified.191 An implementation may strengthen this implicit exception-specification by adding an explicit one.192

关于c++ - 为什么 vector 被认为是 nothrow_move_constructible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987820/

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