`std :: terminate`如何知道特别处理`std :: exception`s?

PSk*_*cik 2 c++

这个

#include <stdexcept>
struct A /*: public std::exception*/ {
  const char* what() const noexcept { return "this is A";  } 
};
int main(){
  throw A{};
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

给我(上stderr):

terminate called after throwing an instance of 'A'
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

如果我取消注释,死亡消息将变为:

terminate called after throwing an instance of 'A'
what():  this is A
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

怎么std::terminate知道std::exception特别对待s?

我怎么能在自己的模仿中做到这一点set_terminate?我试过了

//...
int main(){
  std::set_terminate([](){
      printf("exception thrown\n");
      std::exception_ptr eptr = std::current_exception();
      std::exception* ptr =  dynamic_cast<std::exception*>(eptr);
      if (ptr) 
        puts(ptr->what());
  });
  throw A{};
}
Run Code Online (Sandbox Code Playgroud)

但它不会因为该dynamic_cast行而编译.

Sam*_*hik 5

最有可能的原因是,如果动态转换成功,它只是尝试将dynamic_cast其转换为a std::exception,并调用虚what()方法.