fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <exception>
  4. #include <typeinfo>
  5.  
  6. class MyException : public std::exception
  7. {
  8. public:
  9. MyException()
  10. : std::exception()
  11. {}
  12. };
  13.  
  14. void hereHappensTheFailure()
  15. {
  16. throw MyException();
  17. }
  18.  
  19. template <class E>
  20. void detector(const E& exception)
  21. {
  22. if (typeid(exception) != typeid(E))
  23. {
  24. std::cout << "Exception was sliced" << std::endl;
  25. }
  26. else
  27. {
  28. std::cout << "Exception was not sliced" << std::endl;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. try
  35. {
  36. hereHappensTheFailure();
  37. }
  38. catch (std::exception ex)
  39. {
  40. detector(ex);
  41. }
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Exception was not sliced