title: Get typename of derived class from base instance in C++
body: <p>I am creating a generic error handler for a C++ project. As part of the logging, I want to include the name of the exception class. I'm hoping there's a way to generically get the name of the specific error class from a std::exception instance without having to use dynamic_cast and a logic tree.</p>
  
  <p>Example:</p>
  
  <p><strong>exception_handler.h</strong></p>
  
  <pre><code>#pragma once
  
  #include &lt;exception&gt;
  #include &lt;string&gt;
  
  class ExceptionHandler
  {
  public:
      static std::string get_exception_type_name(std::exception ex)
      {
          return ((std::string)typeid(ex).name()).substr(11);
      }
  };
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "exception_handler.h"
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::string any = "any";
      std::out_of_range ex("Out of range exception");
  
      std::cout &lt;&lt; ExceptionHandler::get_exception_type_name(ex) &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "Press any key to close this window..." &lt;&lt; std::endl;
      std::cin &gt;&gt; any; 
  }   
  </code></pre>
  
  <p>Executing outputs "exception". I want it to say "out_of_range" or whatever other kind of derived exception I feed into the function.</p>
  
  <p>Thanks in advance.</p>