是否有机会调用std::vector<T>::clear()
引发异常?
最佳答案
编号
如果我的元素类型析构函数抛出异常怎么办?
在C++ 11中,std::vector<T>::clear()
标记为noexcept
([n3290: 23.3.6/1]
)。
实现可能会捕获~T
掉出的任何异常,因此clear()
本身可能不会抛出任何异常。如果不是,那么确实是这样,则异常是“意外的”,并且终止了进程而不是传播:
struct T {
~T() { throw "lol"; }
};
int main() {
try {
vector<T> v{T()};
v.clear();
}
catch (...) {
cout << "caught";
}
}
// Output: "terminated by exception: lol" (GCC 4.7.0 20111108)
关于c++ - vector <T>::: clear可以抛出吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8065976/