fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. class someOtherClass
  5. {
  6. public:
  7. someOtherClass()
  8. : a(0)
  9. , b(1.0f)
  10. , c(2.0)
  11. {}
  12. int a;
  13. float b;
  14. double c;
  15. };
  16.  
  17. class logger
  18. {
  19. public:
  20. // Specific case for handling a complex object
  21. logger& operator << ( const someOtherClass& rObject )
  22. {
  23. std::cout << rObject.a << std::endl;
  24. std::cout << rObject.b << std::endl;
  25. std::cout << rObject.c << std::endl;
  26. return *this;
  27. }
  28.  
  29. // [other class specific implementations]
  30.  
  31. // Template for handling pointers which might be null
  32. template< typename _T >
  33. logger& operator << ( const _T* pBar )
  34. {
  35. if ( pBar )
  36. {
  37. std::cout << "Pointer handled:" << std::endl;
  38. return *this << *pBar;
  39. }
  40. else
  41. std::cout << "null" << std::endl;
  42. return *this;
  43. }
  44.  
  45. // Template for handling simple types.
  46. template< typename _T , typename = typename ::std::enable_if_t<!std::is_pointer<_T>::value> >
  47. logger& operator << ( const _T& rBar )
  48. {
  49. std::cout << "Reference: " << rBar << std::endl;
  50. return *this;
  51. }
  52. };
  53.  
  54. int main(int argc, char* argv[])
  55. {
  56. logger l;
  57. someOtherClass soc;
  58. someOtherClass* pSoc = &soc;
  59. l << soc;
  60. l << pSoc;
  61. pSoc = nullptr;
  62. l << pSoc;
  63. return 0;
  64. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
0
1
2
Pointer handled:
0
1
2
null