提问人:LoS 提问时间:10/6/2023 最后编辑:LoS 更新时间:10/6/2023 访问量:122
为什么 clear() 函数是 noexcept 而析构函数不是?
Why the clear() function is noexcept while the destructor is not?
问:
我注意到,在分配器感知容器接口中,标准要求成员函数是,而析构函数不是。此外,在某些基于节点的容器(例如 和 )的情况下,它似乎存在差异,因为成员函数执行与析构函数相同的操作。这种差异有什么原因吗?clear()
noexcept
std::forward_list
std::set
clear()
答:
7赞
Mestkon
10/6/2023
#1
析构函数是隐式的,除非任何成员变量析构函数或基类析构函数已用 标记,或者析构函数本身显式标记为 。noexcept
noexcept(false)
noexcept(false)
评论
noexcept
noexcept(false)
[res.on.exception.handling]/3
,它声称所有标准库析构函数都是 .noexcept
clear()
erase(begin(), end())
clear()