fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template<typename T>
  5. class Vector {
  6. private:
  7. T x, y, z;
  8.  
  9. public:
  10. Vector(T x, T y, T z) : x(x), y(y), z(z) {}
  11. // constructors, destructors, etc.
  12.  
  13. T X() const { return ( this->x ); }
  14. T Y() const { return ( this->y ); }
  15. T Z() const { return ( this->z ); }
  16.  
  17. template<typename U> auto operator+(const Vector<U> &v) const -> Vector<decltype(this->x + v.X())>
  18. {
  19. return ( Vector<decltype(this->x + v.X())>( this->x + v.X(), this->y + v.Y(), this->z + v.Z() ) );
  20. }
  21. };
  22.  
  23. int main() {
  24. Vector<double> d1(1,2,3), d2(4,5,6);
  25. std::cout << (d1 + d2).X();
  26. Vector<int> d3(4,5,6);
  27. std::cout << (d1 + d3).X();
  28. }
  29.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
55