无论指针类型如何,都必须将“throw nullptr”作为指针捕获吗?
Must `throw nullptr` be caught as a pointer, regardless of pointer type?
以下程序抛出 nullptr
然后捕获异常 int*
:
#include <iostream>
int main() {
try {
throw nullptr;
}
catch(int*) {
std::cout << "caught int*";
}
catch(...) {
std::cout << "caught other";
}
}
在 Clang 和 GCC 中程序成功打印 caught int*
,演示:https://gcc.godbolt.org/z/789639qbb
但是在 Visual Studio 16.11.2 中程序打印 caught other
。是 MSVC 中的错误吗?
根据标准 [except.handle]:
,Visual Studio 看起来像是一个错误
A handler is a match for an exception object of type E
if
[...]
- the handler is of type
cv T
or const T&
where T
is a pointer
or pointer-to->member
type and E
is std::nullptr_t
.
以下程序抛出 nullptr
然后捕获异常 int*
:
#include <iostream>
int main() {
try {
throw nullptr;
}
catch(int*) {
std::cout << "caught int*";
}
catch(...) {
std::cout << "caught other";
}
}
在 Clang 和 GCC 中程序成功打印 caught int*
,演示:https://gcc.godbolt.org/z/789639qbb
但是在 Visual Studio 16.11.2 中程序打印 caught other
。是 MSVC 中的错误吗?
根据标准 [except.handle]:
,Visual Studio 看起来像是一个错误A handler is a match for an exception object of type
E
if[...]
- the handler is of type
cv T
orconst T&
whereT
is apointer
orpointer-to->member
type andE
isstd::nullptr_t
.