Guest User

Untitled

a guest
Feb 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. {1.f, 2.f, 3.f} * 2.f = {2.f, 4.f, 6.f}
  2. {1.f, 0.f} + {0.f, 1.f} = {1.f, 1.f}
  3.  
  4. template<class T, size_t N>
  5. class Vec : public std::array<T, N>
  6. {
  7. friend Vec<T, N>& operator*=(Vec<T, N>& a, const Vec<T, N>& b)
  8. {
  9. std::transform(a.begin(), a.end(), b.begin(), a.begin(), std::multiplies<>());
  10. return a;
  11. }
  12.  
  13. template<class S>
  14. friend Vec<T, N>& operator*=(Vec<T, N>& a, const S& b)
  15. {
  16. std::transform(a.begin(), a.end(), a.begin(), [&] (T x) { return x*b; });
  17. return a;
  18. }
  19.  
  20. template<class S>
  21. friend Vec<T, N> operator*(Vec<T, N> a, const S& b)
  22. {
  23. return a *= b;
  24. }
  25. };
  26. using Vec2 = Vec<float, 2>;
  27.  
  28. Vec2 a{1.f, 1.f};
  29. auto b = a * 0.5f; // b = {.5f, .5f} <- as expected
  30. auto c = 0.5f * a; // c = {.5f, 0.f} <- what happened here?
Add Comment
Please, Sign In to add comment