C++标准typeof关键字应用

3600阅读 0评论2015-11-10 evaspring
分类:C/C++


点击(此处)折叠或打开

  1. #include <iostream>

  2. class Test1
  3. {
  4. public:
  5.     static void getuid()
  6.     {
  7.         std::cout << "has getuid func!" << std::endl;
  8.     }
  9. };

  10. class Test2
  11. {
  12. };

  13. template<typename T, bool b>
  14. struct TBaseImpl
  15. {
  16. };

  17. template<typename T>
  18. struct TBaseImpl<T, true>
  19. {
  20.     static void getuid()
  21.     {
  22.         T::getuid();
  23.     }
  24. };

  25. template<typename T>
  26. struct TBaseImpl<T, false>
  27. {
  28.     static void getuid()
  29.     {
  30.         std::cout << "not found getuid!" << std::endl;
  31.     }
  32. };

  33. template<typename T>
  34. class has_getuid
  35. {
  36. public:
  37.     template<typename C>
  38.     static char test(typeof(&C::geuid));

  39.     template<typename C>
  40.     static long test(...);

  41.     enum { value = sizeof(test<T>(0)) == sizeof(char) };
  42. };

  43. int main()
  44. {
  45.     TBaseImpl<Test2, has_getuid<Test2>::value>::getuid();
  46.     TBaseImpl<Test1, has_getuid<Test1>::value>::getuid();
  47.     return 0;
  48. }


上一篇:TEMP_FAILURE_RETRY宏的应用
下一篇:linux中send recv 函数 MSG_NOSIGNAL异常消息