fork download
  1. #include <iostream>
  2.  
  3. class Rectangle
  4. {
  5. public:
  6. Rectangle(double w = 0, double h = 0, double r = 0) : width(w), height(h), radius(r)
  7. {}
  8.  
  9. double width;
  10. double height;
  11. double radius;
  12. };
  13.  
  14. namespace traits
  15. {
  16. template <typename P>
  17. struct operations
  18. {
  19. static void display(Rectangle const &, std::ostream &);
  20. static double area(Rectangle const &);
  21. };
  22.  
  23. template <typename P, int N>
  24. struct access {};
  25. }
  26.  
  27. namespace traits
  28. {
  29. template <int N>
  30. struct access<Rectangle, N>
  31. {
  32. static double get(Rectangle const &);
  33. };
  34. }
  35.  
  36. namespace traits
  37. {
  38. template <>
  39. struct access<Rectangle, 0>
  40. {
  41. static double get(Rectangle const &rect)
  42. {
  43. return rect.width;
  44. }
  45. };
  46.  
  47. template <>
  48. struct access<Rectangle, 1>
  49. {
  50. static double get(Rectangle const &rect)
  51. {
  52. return rect.height;
  53. }
  54. };
  55. }
  56.  
  57. namespace traits
  58. {
  59. template <>
  60. struct operations<Rectangle>
  61. {
  62. static void display(Rectangle const &rect, std::ostream &os)
  63. {
  64. os << "Width of rectangle: " << rect.width << std::endl;
  65. os << "Height of rectangle: " << rect.height << std::endl;
  66. os << "Area of rectangle: " << area(rect) << std::endl;
  67. }
  68. static double area(Rectangle const& rect)
  69. {
  70. double width = access<Rectangle, 0>::template get<0>(rect);
  71. double height = access<Rectangle, 1>::template get<1>(rect);
  72.  
  73. return width * height;
  74. }
  75. };
  76. }
  77.  
  78. template <int N, typename P>
  79. static inline double get(P const &p)
  80. {
  81. return traits::access<P, N>::get(p);
  82. }
  83.  
  84. template <typename P>
  85. static inline void display(P const &p)
  86. {
  87. traits::operations<P>::display(p, std::cout);
  88. }
  89.  
  90. template <typename P>
  91. static inline double area(P const &p)
  92. {
  93. return traits::operations<P>::area(p);
  94. }
  95.  
  96. /*ostream & operator<<(ostream & out, Shape const& s)
  97. {
  98.   s.display(out);
  99.   return out;
  100. }*/
  101.  
  102.  
  103. int main()
  104. {
  105.  
  106. }
Compilation error #stdin compilation error #stdout 0s 2848KB
stdin
Standard input is empty
compilation info
prog.cpp: In static member function ‘static double traits::operations<Rectangle>::area(const Rectangle&)’:
prog.cpp:70:52: error: ‘template’ (as a disambiguator) is only allowed within templates
prog.cpp:70:72: error: no match for ‘operator>’ in ‘false > rect’
prog.cpp:70:72: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
prog.cpp:71:52: error: ‘template’ (as a disambiguator) is only allowed within templates
prog.cpp:71:65: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
prog.cpp:71:72: error: no match for ‘operator>’ in ‘(traits::access<Rectangle, 1>::get == 0u) > rect’
prog.cpp:71:72: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
stdout
Standard output is empty