fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. using namespace std;
  5.  
  6. class CBase{
  7. public:
  8. void basefunc(){
  9. cout << "basefunc" << endl;
  10. }
  11. };
  12.  
  13.  
  14. template<typename T,bool is_derived_from_base>
  15. class Hoge_impl; // 何も定義しない
  16.  
  17. // 特殊化
  18. template<typename T>
  19. class Hoge_impl<T,true>{
  20. T t;
  21. public:
  22. void func(){
  23. t.basefunc();
  24. }
  25. };
  26.  
  27. template<typename T>
  28. class Hoge : private Hoge_impl<T,std::is_base_of<CBase,T>::value>{
  29. public:
  30. using Hoge_impl<T,std::is_base_of<CBase,T>::value>::func;
  31. };
  32.  
  33. class CDerived1 : public CBase{
  34. };
  35. class CDerived2 : public CBase{
  36. };
  37. class CNotDerived{
  38. };
  39.  
  40. int main()
  41. {
  42. //これは通る。
  43. Hoge<CDerived1> hoge1;
  44. Hoge<CDerived2> hoge2;
  45. //が、これは通らない。
  46. //Hoge<CNotDerived> hoge3;
  47. // Hoge<int> hoge4;
  48.  
  49. hoge1.func();
  50. hoge2.func();
  51. // hoge3.func();
  52. // hoge4.func();
  53. return 0;
  54. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
basefunc
basefunc