Get reference to std::exception held by std::exception_ptr
04:51 17 May 2021

I am trying to get reference to std::exception which is held by std::exception_ptr.

Here's what I have tried:

#include 

using namespace std::literals;

auto unwrap(std::exception_ptr ptr) -> const std::exception &
{
    try {
        std::rethrow_exception(ptr);
    } catch (std::exception &e) {
        return e;
    }
}

int main()
{
    const auto ptr = std::make_exception_ptr(std::runtime_error{"test"});
    const auto &exp = unwrap(ptr);
    std::cout << exp.what() << std::endl;
    return 0;
}

With GCC, stdout shows "test" which is what I expected. However, with MSVC 2019, stdout shows "Unknown exception".

Is there any undefined, unspecified, or implementation dependent behavior in my source code? Or, is it a compiler/standard library implementation bug?

c++ error-handling