Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312720
  • 博文数量: 11
  • 博客积分: 4329
  • 博客等级: 上校
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-01 12:03
文章分类

全部博文(11)

文章存档

2016年(4)

2015年(7)

分类: C/C++

2015-11-10 11:57:43


点击(此处)折叠或打开

  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. }


阅读(3494) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~