Home > other >  Must `throw nullptr` be caught as a pointer, regardless of pointer type?
Must `throw nullptr` be caught as a pointer, regardless of pointer type?

Time:11-04

The following program throws nullptr and then catches the exception as int*:

#include <iostream>

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

In Clang and GCC the program successfully prints caught int*, demo: https://gcc.godbolt.org/z/789639qbb

However in Visual Studio 16.11.2 the program prints caught other. Is it a bug in MSVC?

CodePudding user response:

Looks like a bug in Visual Studio, according to the standard [except.handle]:

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.
  • Related