#include #include // for min() #include using namespace std; template class mathvector { public: explicit mathvector(array const& values) : content_(values) {} explicit mathvector(array && values) : content_(move(values)) {} mathvector() : content_{} {} // zero-initialized mathvector& operator*=(Type const& scalar) { for (auto& component : content_) component *= scalar; return *this; } mathvector& operator+=(mathvector const& other) { for (size_t i(0); i < Dim; ++i) content_[i] += other.content_[i]; return *this; } template mathvector convert() const { array other{}; constexpr size_t upper(min(Dim, Dim2)); for (size_t i(0); i < upper; ++i) other[i] += content_[i]; return mathvector(other); } void display(ostream& out) const { out << '('; if (Dim > 0) { constexpr size_t upper(Dim - 1); for (size_t i(0); i < upper; ++i) out << content_[i] << ", "; out << content_[upper]; } out << ')'; } private: array content_; }; template mathvector operator*(Type const& scalar, mathvector vect) { vect *= scalar; return vect; } template mathvector operator+(mathvector vect1, mathvector const& vect2) { vect1 += vect2; return vect1; } template ostream& operator<<(ostream& out, mathvector const& vect) { vect.display(out); return out; } int main() { mathvector<2> v1({3.4, 4.5}); mathvector<4, int> v2({3, 4, 5, 6}); // implicit cannot work here mathvector<3, int> v3(v2.convert<3>()); mathvector<4, double> v4(v1.convert<4>()); cout << v1 << endl; cout << v2 << endl; cout << v3 << endl; cout << v4 << endl; cout << -2 * v2 << endl; cout << v3 + v3 << endl; return 0; }