Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <type_traits>
- template<typename T>
- struct Coord
- {
- using type = T;
- T x, y;
- Coord(T x = 0, T y = 0) : x(x), y(y) {}
- };
- template<typename T>
- struct Point_b : Coord<T>
- {
- Point_b(T x = 0, T y = 0) : Coord<T>(x, y) {}
- };
- template<typename T>
- struct Vec_b : Coord<T>
- {
- Vec_b(T x = 0, T y = 0) : Coord<T>(x, y) {}
- };
- template<typename U, typename T = typename U::type>
- struct CoordOps : U
- {
- static_assert(std::is_base_of<Coord<T>, U>::value, "not a Coord-based class");
- CoordOps(T x = 0, T y = 0) : U(x, y) {}
- U& operator+=(const Vec_b<T>& r)
- {
- this->x += r.x;
- this->y += r.y;
- return *this;
- }
- U operator+(const Vec_b<T>& r)
- {
- U l(*this);
- l.x += r.x;
- l.y += r.y;
- return l;
- }
- };
- template<typename T> using Point = CoordOps<Point_b<T>>;
- template<typename T> using Vec = CoordOps<Vec_b<T>>;
- int main()
- {
- using Vecd = Vec<double>;
- using Pointd = Point<double>;
- Vecd v1, v2;
- Pointd p1, p2;
- v1 += v2;
- auto v3 = v1 + v2;
- p1 += v3;
- p1 += p2; // błąd
- v3 += p2; // błąd
- auto p3 = p1 + p2; // błąd
- auto p4 = v1 + p1; // błąd
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment