无论指针类型如何,都必须将“throw nullptr”捕获为指针吗?

Fed*_*dor 41 c++ exception language-lawyer

以下程序抛出nullptr异常,然后捕获异常int*

#include <iostream>

int main() {
    try {
        throw nullptr;
    }
    catch(int*) {
        std::cout << "caught int*";
    }
    catch(...) {
        std::cout << "caught other";
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Clang 和 GCC 中,程序成功打印caught int*, demo: https://gcc.godbolt.org/z/789639qbb

但是在 Visual Studio 16.11.2 中,程序打印caught other. 这是 MSVC 中的错误吗?

ale*_*ame 39

根据标准[except.handle],看起来像是 Visual Studio 中的错误:

\n
\n

E处理程序与类型为if的异常对象匹配

\n

[...]

\n
    \n
  • 处理程序的类型为cv Torconst T&其中T是 apointerpointer-to->member类型并且Estd\xe2\x80\x8b::\xe2\x80\x8bnullptr_t
  • \n
\n
\n

  • 对应的GCC [bug报告](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58796)。这已在 GCC 7 中修复。 (3认同)