fork download
  1. #include<iostream>
  2. class HasFoo
  3. {
  4. public :
  5.  
  6. typedef char (&Small)[1];
  7. typedef char (&Large)[2];
  8.  
  9. template <typename C, void (C::*) ()> class SFINAE {};
  10.  
  11. template <typename C> static Small test (SFINAE<C, &C::foo> *)
  12. {
  13. std::cout << "HAS" << std::endl;
  14. }
  15.  
  16. template <typename C> static Large test (...)
  17. {
  18. std::cout << "NOT HAS" << std::endl;
  19. }
  20. };
  21.  
  22. class B
  23. {
  24. public :
  25.  
  26. void foo () {}
  27. };
  28.  
  29. class D1 : public B
  30. {
  31. private: void foo () {} // override
  32. };
  33.  
  34. class D2 : public B
  35. {
  36. public :
  37.  
  38. using B::foo;
  39. };
  40.  
  41. class D3 : public B {};
  42.  
  43. /////////////////////////////////////////////////////////////////
  44.  
  45. template<typename C, void (C::*p)()>
  46. struct X{};
  47.  
  48. template struct X<D1,&D1::foo>; //<--- private members can be used in explicit instantiation
  49.  
  50. //////////////////////////////////////////////////////////////////
  51.  
  52. int main ()
  53. {
  54. // HasFoo::test<B>(0);
  55. // HasFoo::test<D1>(0);
  56. // HasFoo::test<D2>(0);
  57. // HasFoo::test<D3>(0);
  58. }
Success #stdin #stdout 0.02s 2676KB
stdin
Standard input is empty
stdout
Standard output is empty