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