std::priority_queue::pop 什么时候可以抛出异常
When could std::priority_queue::pop throw an exception
std::priority_queue
的 pop()
方法未声明 [=13=],因此理论上可以抛出异常。但它什么时候会抛出异常,这些异常可能是什么?
它可以标记为 nothrow
,但不是。
为什么std::priority_queue::pop
可以*不可以
void pop();
Removes the top element from the priority queue. Effectively calls
std::pop_heap(c.begin(), c.end(), comp); c.pop_back();
c
默认是 std::vector
.
void pop_back();
4/ Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.
5/ Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T.
*所以只有 T
的析构函数被调用并且不能抛出因为
[requirements.on.functions]/2.4
2/ In particular, the effects are undefined in the following cases:
[...]
2.4/ if any replacement function or handler function or destructor operation exits via an exception, unless specifically allowed in the applicable Required behavior: paragraph.
为什么 std::priority_queue::pop
不是 nothrow
?
由于从 T::~T
抛出的异常会导致 UB,实现可以假设它不会发生并且仍然符合标准。另一种处理方法是让这样的库函数nothrow(false)
而不处理它。
std::priority_queue
的 pop()
方法未声明 [=13=],因此理论上可以抛出异常。但它什么时候会抛出异常,这些异常可能是什么?
它可以标记为 nothrow
,但不是。
为什么std::priority_queue::pop
可以*不可以
void pop();
Removes the top element from the priority queue. Effectively calls
std::pop_heap(c.begin(), c.end(), comp); c.pop_back();
c
默认是 std::vector
.
void pop_back();
4/ Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.
5/ Throws: Nothing unless an exception is thrown by the assignment operator or move assignment operator of T.
*所以只有 T
的析构函数被调用并且不能抛出因为
[requirements.on.functions]/2.4
2/ In particular, the effects are undefined in the following cases:
[...]
2.4/ if any replacement function or handler function or destructor operation exits via an exception, unless specifically allowed in the applicable Required behavior: paragraph.
为什么 std::priority_queue::pop
不是 nothrow
?
由于从 T::~T
抛出的异常会导致 UB,实现可以假设它不会发生并且仍然符合标准。另一种处理方法是让这样的库函数nothrow(false)
而不处理它。