fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <stdexcept>
  4. #include <utility>
  5.  
  6. template <class Object, class MemFun, class... Args>
  7. struct HasNoExceptMemFun
  8. {
  9. static constexpr bool value =
  10. noexcept (((std::declval <Object> ()).*(MemFun ()))(std::declval <Args>()...));
  11. };
  12.  
  13. template <bool which>
  14. class Is;
  15.  
  16. template <>
  17. struct Is <false>
  18. {
  19. void operator () ()
  20. {
  21. std::cout << "false" << std::endl;
  22. }
  23. };
  24.  
  25. template <>
  26. struct Is <true>
  27. {
  28. void operator () ()
  29. {
  30. std::cout << "true" << std::endl;
  31. }
  32. };
  33.  
  34. class Foo
  35. {
  36. Foo (int) {}
  37.  
  38. public:
  39.  
  40. void Baz (int) noexcept (true)
  41. {
  42. }
  43. };
  44.  
  45. class Bar
  46. {
  47. public:
  48.  
  49. void Baz (int) throw (std::runtime_error)
  50. {
  51. throw std::runtime_error ("help!");
  52. }
  53. };
  54.  
  55. template <typename T, typename F>
  56. struct selection
  57. {
  58. static void dispatch ()
  59. {
  60. Is <HasNoExceptMemFun <T, F, int>::value> () ();
  61. }
  62. };
  63.  
  64. int main() {
  65. selection <Foo, decltype (&Foo::Baz)>::dispatch ();
  66. selection <Bar, decltype (&Bar::Baz)>::dispatch ();
  67. return 0;
  68. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
true
false