fork(2) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. #include <boost/tti/has_member_function.hpp>
  5. #include <boost/function_types/result_type.hpp>
  6.  
  7. struct Interface
  8. {
  9. std::string interface()const
  10. {
  11. return "interface";
  12. }
  13. };
  14.  
  15. struct my_interface
  16. {
  17. std::string get_my_interface()const
  18. {
  19. return "get_my_interface";
  20. }
  21. };
  22.  
  23. struct myInterfaceType
  24. {
  25. std::string myInterface()const
  26. {
  27. return "myInterface";
  28. }
  29. };
  30.  
  31. namespace foo
  32. {
  33. namespace detail
  34. {
  35. BOOST_TTI_HAS_MEMBER_FUNCTION(interface)
  36. BOOST_TTI_HAS_MEMBER_FUNCTION(get_my_interface)
  37. BOOST_TTI_HAS_MEMBER_FUNCTION(myInterface)
  38. }
  39.  
  40. template<typename Interface>
  41. struct GenericInterface
  42. {
  43. template<typename T = Interface>
  44. typename std::enable_if< detail::has_member_function_interface<std::string (T::*)()const>::value,std::string >::type query_interface()const
  45. {
  46. return i.interface();
  47. }
  48. template<typename T = Interface>
  49. typename std::enable_if< detail::has_member_function_get_my_interface<std::string (T::*)()const >::value, std::string>::type query_interface()const
  50. {
  51. return i.get_my_interface();
  52. }
  53. template<typename T = Interface>
  54. typename std::enable_if< detail::has_member_function_myInterface<std::string (T::*)()const >::value, std::string >::type query_interface()const
  55. {
  56. return i.myInterface();
  57. }
  58. void print()
  59. {
  60. std::cout << query_interface() << "\n";
  61. }
  62.  
  63. private:
  64. Interface i;
  65. };
  66. }
  67.  
  68. int main(int argc, char *argv[])
  69. {
  70. foo::GenericInterface<Interface> gInterface;
  71. foo::GenericInterface<my_interface> gmy_interface;
  72. foo::GenericInterface<myInterfaceType> gmyInterface;
  73.  
  74. gInterface.print();
  75. gmy_interface.print();
  76. gmyInterface.print();
  77.  
  78. std::cout << std::boolalpha <<foo::detail::has_member_function_get_my_interface<std::string (my_interface::*)()const>::value << std::endl;
  79. std::cout << std::boolalpha <<foo::detail::has_member_function_interface<myInterfaceType,std::string>::value << std::endl;
  80. std::cout << std::boolalpha << foo::detail::has_member_function_interface<std::string (Interface::*)()const>::value << std::endl;
  81.  
  82. static_assert(foo::detail::has_member_function_interface<std::string (Interface::*)()const>::value,"fail");
  83.  
  84. std::cout << "Hello World!" << std::endl;
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
interface
get_my_interface
myInterface
true
false
true
Hello World!