fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <typename T>
  5. class example{
  6. private:
  7. T value;
  8. public:
  9. example(){ value=(T)0; }
  10. example(const T& rhs){ value=rhs; }
  11. ~example(){}
  12.  
  13. T val() const { return value; }
  14. };
  15.  
  16. template <typename T> inline class example<T> operator+(const class example<T>& lhs, const class example<T>& rhs){ return example<T>(lhs.val()+rhs.val()); }
  17. template <typename T> inline class example<T> operator+(const class example<T>& lhs, const T & rhs){ return example<T>(lhs.val()+rhs ); }
  18. template <typename T> inline class example<T> operator+(const T & lhs, const class example<T>& rhs){ return example<T>(lhs +rhs.val()); }
  19.  
  20. template <typename T>
  21. void print(const class example<T>& rhs){ std::cout<<" = "<<rhs.val()<<std::endl; }
  22.  
  23. #define printn(var) {printf("%s", #var);print(var);}
  24. #define printn_all(var) {printf("%s(%d): ", __func__, __LINE__);printf("%s", #var);print(var);}
  25.  
  26. int main(){
  27. class example<double> v1(1.0), v2(2.0), v3(3.0), buf;
  28. printn(v1);
  29. printn(v2);
  30. printn(v3);
  31. printn(v1+v2+v3);
  32.  
  33. buf=0.1+v1+v2+v3+4.0;
  34. printn(buf);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
v1 = 1
v2 = 2
v3 = 3
v1+v2+v3 = 6
buf = 10.1